Express redirect does not change req.url

I have a route that is redirected upon successful login

app.post('/login', function(req, res){
  if(req.body.password === Server.cfg.auth.userPass) {
    req.session.user = {nick: req.body.username, pass: req.body.password}
    res.redirect('/chat')
  } else {
    res.render('user/login', { locals: { error: 'Invalid password' } })
  }
})

The redirect seems to work as the page is being refreshed using a properly processed jade file. However, url still says / login, and my pageTitle variable (set via boilerplate vars) does not change either. If I refresh the page after the redirect, everything will change as it should. Only after redirection does it not change.

+5
source share
2 answers

, ajax, . , , , ; .., JavaScript .

, jquery ajax :

model.save({ error:function... 

function requiresLogin(req, res, next) {  
    if(req.session.user) {
        next();
    } else {
        //res.redirect('/sessions/new?redir=' + req.url); // won't work
        res.send("Unauthorized", 403); // send 403 http status
    }
}

Client

  // Assumes you can get http status from response
  error: function(resp, status, xhr)...
      if(resp.status === 403) {
          window.location = '/sessions/new'; // optionally put a redirLastPage=somewhere
      }

, . googling ajax, ,

+1

All Articles