Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

371
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda