Why is PassportJS in Node not removing session on logout

前端 未结 22 1899
有刺的猬
有刺的猬 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 23:02

    Since you are using passport authentication which uses it's own session via the connect.sid cookie this simplest way of dealing with logging out is letting passport handle the session.

    app.get('/logout', function(req, res){
      if (req.isAuthenticated()) {
        req.logOut()
        return res.redirect('/') // Handle valid logout
      }
    
      return res.status(401) // Handle unauthenticated response
    })
    

提交回复
热议问题