der*_*rek 5 session node.js express passport.js
更新:
它的行为与我的实际FB登录一致.当我退出我的Facebook然后点击我网站上的"登录"按钮时,它会将我重定向到Facebook登录页面并要求我登录.之后,我正确回到我网站上的"profile.html"网页.但是,当我从我的网站点击"退出"时,它会转到我网站的主页.这次,当我再次点击"登录"按钮时,它直接进入我网站的"profile.html".似乎最后一次"退出"根本不起作用."注销"只能在我退出我的Facebook帐户时才能使用.所以在我的网站上使用的会话依赖于facebook的会话.很奇怪!
我正在使用PassportJS来完成我的身份验证工作.但我发现req.logout()或req.session.destroy()根本不起作用.
// route for showing the profile page
app.get('/login', isLoggedIn, function(req, res) {
res.render('profile', {
user : req.user // get the user out of session and pass to template
});
});
// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated()){
console.log("req is authenticated!");
return next();
}
// if they aren't redirect them to the home page
res.redirect('/');
}
// route for logging out
app.get('/logout', function(req, res) {
console.log("logging out!");
req.logout();
req.session.destroy();
res.redirect('/');
});
Run Code Online (Sandbox Code Playgroud)
当我登出注销时,我可以看到"注销"消息.然后我被重定向到主页.当我再次点击登录时,我看不到任何登录窗口,直接进入"个人资料"页面.在此过程中,我确实看到"req已经过身份验证!" 信息.
我的问题:
1:"req.isAuthenticated()"在哪里?它为什么总是如此?
2:为什么"req.logout()"或"req.session.destroy()"不起作用?
谢谢
德里克
req.isAuthenticated()是护照的一部分。相关代码:
req.isAuthenticated = function() {
var property = 'user';
if (this._passport && this._passport.instance._userProperty) {
property = this._passport.instance._userProperty;
}
return (this[property]) ? true : false;
};
Run Code Online (Sandbox Code Playgroud)
检查属性并返回布尔值。
req.logout()删除该属性,以便在以后的请求中返回 false。
同时,session.destroy来自expressjs/session中间件,因此与passport无关。也许您正在索引页面中再次创建会话。该问题需要更多信息。
| 归档时间: |
|
| 查看次数: |
5076 次 |
| 最近记录: |