req.logout()或req.session.destroy()不起作用

更新:

它的行为与我的实际FBlogin一致。 当我注销我的Facebook,然后单击我的网站上的“login”button,它将我redirect到Facebooklogin页面,并要求我login。 之后,我正确地回到我的网站上的“profile.html”网页。 但是,当我从我的网站点击“注销”时,它会进入我网站的主页。 这一次,当我再次点击“login”button,它直接进入我的网站“profile.html”。 看来最后的“注销”根本不起作用。 “注销”只能在我注销我的Facebook帐户时才起作用。 所以在我的网站上使用的会话依赖于Facebook的会话。 很奇怪!


我正在使用PassportJS来完成我的身份validation作业。 但是我发现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('/'); }); 

当我坚持注销时,我可以看到“注销”消息。 然后我被redirect到主页。 当我再次点击login,我看不到任何login窗口,直接进入“个人资料”页面。 在这个过程中,我确实看到“请求被authentication!” 信息。

我的问题:

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; }; 

检查属性并返回一个布尔值。


req.logout()移除该属性,以便在将来的请求中返回false。


同时,session.destroy来自expressjs / session中间件,所以不是护照相关的。 也许你正在索引页面中再次创build会话。 这个问题需要更多的信息。