Why is PassportJS in Node not removing session on logout

前端 未结 22 1883
有刺的猬
有刺的猬 2020-11-28 22:11

I am having trouble getting my system to log out with PassportJS. It seems the logout route is being called, but its not removing the session. I want it to return 401, if th

22条回答
  •  心在旅途
    2020-11-28 22:55

    Brice’s answer is great, but I still noticed an important distinction to make; the Passport guide suggests using .logout() (also aliased as .logOut()) as such:

    app.get('/logout', function(req, res){
      req.logout();
      res.redirect('/'); //Can fire before session is destroyed?
    });
    

    But as mentioned above, this is unreliable. I found it behaved as expected when implementing Brice’s suggestion like this:

    app.get('/logout', function (req, res){
      req.session.destroy(function (err) {
        res.redirect('/'); //Inside a callback… bulletproof!
      });
    });
    

    Hope this helps!

提交回复
热议问题