0
votes

How can I redirect the user to the login page and after the user logs in and based on a boolean variable again redirect the user to any url?

For instance:

app.get('*', (req, res, next) => {
  if(req.path === ''){
    res.redirect(302, '/login')

//After login in, I need to check the boolean flag and based on that redirect the user to a specific url
    if(flag) {
      res.redirect(302, '/error')
    }
  } else {
    next();
  }
})

With this implementation I'm getting the following error:

'Error [ERR_HTTP_HEADERS_SENT] Cannot set headers after they are sent to the client'

Any suggestion?

1

1 Answers

0
votes

You are sending 2 response if your flag is true. You should do:

app.get('*', (req, res, next) => {
    if(req.path === ''){
        if(flag) {
           res.redirect(302, '/error')
        } else {
           res.redirect(302, '/login')
        }
  } else {
    next();
  }
})