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

296
Views
redirect user if not logged in (Firebase/ Vanila JS)

Good night, I need help with a problem that I'm not able to solve, I have a site with the index, register and login page, my problem is that I want that when the user is logged out he is redirected to the registration page if no, it is redirected to index, but I'm not able to do this without generating an infinite loop, could someone help me?

Meu código

firebase.auth().onAuthStateChanged(user => {

if (user) {
    var uid = user.uid;
    console.log("usuario conectado")
    
} else {
   window.location = 'register.html'
}})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think you should check the current location that the user currently is before doing redirect.

firebase.auth().onAuthStateChanged(user => {
  if (user) {
    var uid = user.uid;
    console.log("usuario conectado")
    
} else {
  if (window.location.pathname === '/login.html') return // user is in login page, skip.
  if (window.location.pathname !== '/register.html') {
    window.location = 'register.html'
  }
 }
}
about 4 years ago · Juan Pablo Isaza 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!