Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

370
Visualizações
Redirect even if user uses back button (Node.js)

I have made a login system with jwt and, basically, the user redirects to the homepage (homepage/) if login (homepage/login) is successfull. After that, even if the user types (homepage/login) in the url, they get redirected back to the homepage because (and as long as) they have the jwt token.

I have the following code:

app.get('/login', (req, res) => {
  const token = req.cookies.jwt
  if(token) {
    res.redirect('/')
  } else {
    res.render('login')
  }
})

HOWEVER, if the user goes to (homepage/login) through the "back button", they are no longer redirected even though they have the token. How can I include a redirect if the back button is pressed ?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Redirection is happening on client side.

So, simply include a frontend javascript code that checks if token is there. If not present, redirect.

In html code, include this script in head tag (assuming you are using localstorage to store token:

<html>
  <head>
  ..
  ..
  <script>
    const token = localstorage.getItem("token");
    if(token == null) {
      window.location.replace("-path-to-home-page-");
    }
    </script>
  </head>
  <body>
  ..
  ..
  ..
  </body>
</html>
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda