Node.js req.logout()或req.session.destroy()都不起作用
更新: 它的行为与我的实际FB登录一致。当我注销facebook,然后单击网站上的“登录”按钮时,它会将我重定向到facebook登录页面并要求我登录。之后,我正确地返回到我网站上的网页“profile.html”。但是,当我从我的网站单击“注销”时,它会进入我的网站主页。这一次,当我再次点击“登录”按钮时,它直接进入我网站的“profile.html”。最后一次“注销”似乎根本不起作用。“注销”只有在我注销我的facebook帐户时才能生效。所以我网站上使用的会话依赖于facebook的会话。很奇怪Node.js req.logout()或req.session.destroy()都不起作用,node.js,session,express,passport.js,Node.js,Session,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('/');
});
当我点击注销时,我可以看到“注销”消息。然后我被重定向到主页。当我再次单击登录时,我看不到任何登录窗口,直接进入“配置文件”页面。在这个过程中,我确实看到了“req已验证!”消息
我的问题是:
1:“req.isAuthenticated()”来自哪里?为什么总是这样
2:为什么“req.logout()”或“req.session.destroy()”不起作用
谢谢
德里克是护照的一部分。相关代码:
req.isAuthenticated = function() {
var property = 'user';
if (this._passport && this._passport.instance._userProperty) {
property = this._passport.instance._userProperty;
}
return (this[property]) ? true : false;
};
检查属性并返回布尔值
删除该属性,以便在将来的请求中返回false
同时,session.destroy来自expressjs/session中间件,因此它与passport无关。可能您正在索引页中再次创建会话。这个问题需要更多的信息