Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

368
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!