5

I have create a web application using sails js. Following is logout function.

logout: function(req, res) {
      req.session.destroy(function(err) {
           res.redirect('/');
      });
}

Sometime user does not logout from one click. But anyway user is redirecting to my home page. Then I have to click again logout button to logout the user.

Gayan Charith
  • 7,301
  • 2
  • 20
  • 32

2 Answers2

5

Send them to a logout page instead of a redirect or put a timeout in the callback.

logout: function(req, res) {
      req.session.destroy(function(err) {
           return res.view('/logout');
      });
}

or

logout: function(req, res) {
      req.session.destroy(function(err) {
           timeout(function(){
               return res.redirect('/');
           }, 1000);
      });
}
Meeker
  • 5,979
  • 2
  • 20
  • 38
  • 1
    ya, depending on the version of sails,express,express-connect their are issues with a redirect. Also depends on how your session is set, if that does not work I would update your question if its a Redis session, or memory or purely cookie? – Meeker Mar 18 '15 at 17:12
  • I tried using above method. But did not work for me. I don't use Redis or cookie. I just assign values to session variables. – Gayan Charith Mar 23 '15 at 08:54
  • Oh, my code above did not RETURN res.view() . I would try that. – Meeker Mar 24 '15 at 13:41
  • This seems to be an unresolved issue, most authentication examples out there are using passport, I am rebuilding a legacy php app in node/sails and i am facing this issue, Sometimes sessions are not destoyed and my policies fail. – ArrowHead Jul 15 '16 at 10:26
  • This continues to work fine for me, however I did stop using sails because the inability to get express updated quick enough which I assume is where this bug lies. – Meeker Jul 15 '16 at 15:33
0

Try this,

logout: function(req, res) {
        // clear login sessions here
        req.session.destroy(function(err) {
          setTimeout(function(){
            return res.redirect('/login');
          }, 2500); // redirect wait time 2.5 seconds
        });
      }

Just giving a delay for sails js to clear the session, Working fine for me...

ReNiSh AR
  • 2,782
  • 2
  • 30
  • 42