0

I am trying to redirect to a login page after successfully logging a user out, but am having an issue when calling res.redirect() from my logout route handler in express.

I have the following middleware to redirect a user to a login page (note I am using express-session too)

//A request to '/login' will serve the login page
app.use('/login', function(req, res){
  res.sendFile(path.join(__dirname+'/public/login.html'))
});

//This will listen for all requests
app.use(function(req, res, next) {
  //Here we only want to redirect to the logic route (above) if the request is not
  //a login action and there is no access token present on the session object
  if (req.url!== '/auth/login' && !req.session.accessToken) {
    res.redirect('/login');
  } else {
    next();
  }
});

This redirect above works as expected.

When a user performs a logout, I handle like so:

app.post('/auth/logout', function(req, res){
    req.session.destroy();
    res.redirect('/login');
  });

I can see in Chrome debugger, the network tab shows my login view is being served back to the browser, but the browser window does not change. I notice the login.html is being returned with a 304 code too if that has any relevance. I'm feeling a bit dumb but am failing to see what's different here, can someone shed some light please?

Thanks

mindparse
  • 6,115
  • 27
  • 90
  • 191
  • What do you use to post the logout request? It might have followed the 304 automatically and the browser is not aware of it. btw I think GET method is more proper for logout. – can. Feb 17 '16 at 13:57
  • I'm performing a POST request over AJAX (using angularJS). Also on first load of the app, I see `http://myapp/login`, then after login I redirect to `'\'` and so see `http://myapp`. But after a logout and calling res.redirect('/login') I just see `http://myapp`, so no `'/login'` unlike when the app first loads – mindparse Feb 17 '16 at 14:00
  • Ah, I think I have found out why - http://stackoverflow.com/questions/11570301/res-redirect-from-post I need to handle this in my angular app – mindparse Feb 17 '16 at 14:04
  • Try res.render('/login') – BrTkCa Feb 17 '16 at 16:22

0 Answers0