Expressjs passport-local can't logout

大城市里の小女人 提交于 2021-02-07 14:22:40

问题


I copy pasted the app passport-local on my app,

The fun is I can log in users, but I can't make them logout,

app.get('/logout', function(req, res){
  req.logout();
  res.redirect('/');
});

this is not doing nothing, nothing on the logfiles, and I I have its a link to /logout

this is the main route examples

app.get('/page1', function(req, res){                                                                                                                       
  res.render('page1', {user: req.user});
});

app.get('*', function(req,res){
  res.render('root', {user: req.user});
});

Why the logout its not working ????


回答1:


Apparently this is a known problem:

Why is PassportJS in Node not removing session on logout

The thread mentioned above suggests to use req.session.destroy() instead.

It would be nice to have some feedback from the Passport team directly.




回答2:


This is still an issue.

What I did was to use req.session.destroy(function (err) {}); on the server side and on the client side, whenever they logout:

const logout = () => {
    const url = '/users/logout'
    fetch(url)
    setTimeout(function () {
      location.reload();    }, 500);

That way, when refreshing the page, the user is without session. Just make sure you are redirecting to the correct page if no one is authenticated.

Not the best approach, perhaps, but it works.



来源:https://stackoverflow.com/questions/12474755/expressjs-passport-local-cant-logout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!