0

I am using express, express-session and passport in my node.js application and have issues when performing an auto logout.

initialization:

expressapp.use(session({
  secret: '****',
  cookie: { maxAge: 1000 * 60 * 60 }, // 1h
  saveUninitialized: false,
  resave: true
}));
expressapp.use(passport.initialize());
expressapp.use(passport.session());

my logout code:

  expressapp.get('/profile/', isLoggedIn, function (req, res) {
    logger.info('get profile view!');
    res.render('profile');
  });

  expressapp.get('/logout', function(req, res) {
    logger.info('logging out..');
    req.session.destroy(function (err) {
        res.redirect('/');
    });
  });

  expressapp.post('/login', passport.authenticate('login', {
    successRedirect: '/#!/profile',
    failureRedirect: '/#!/login',
  }));

  function isLoggedIn(req, res, next) {
    if(req.isAuthenticated()) {
      logger.info('authenticated!');
      return next();
    } else {
      logger.info('unauthorized!');
      res.render('login');
    }
  }

The issue what I now have is if I click on my logout button the function gets called and I get redirected but when going back to profile again the functions listed above do not get called again (no logs printed) as they are still saved somewhere. Only if I reload the site I get redirected to the login page. This does not work for me for the auto logout as I don't reload the page then.

I also tried to call req.logout() in the logout route but with the same result.

How can I force these functions to be called on each click and not on each reload of the site? Where is this information saved?

Tobi
  • 924
  • 1
  • 10
  • 39
  • req.logout does not clear the session. I had a similar issue - you may find the answer here: https://stackoverflow.com/questions/50454992/req-session-destroy-and-passport-logout-arent-destroying-cookie-on-client-side – Michael Nelles Jan 08 '20 at 19:10
  • req.session.destroy() doesn't work either for me – Tobi Jan 08 '20 at 19:21
  • See https://stackoverflow.com/questions/13758207/why-is-passportjs-in-node-not-removing-session-on-logout ? – Cody G Jan 08 '20 at 19:26

0 Answers0