• Jobs
  • About Us
  • Jobs
    • Home
    • Jobs
    • Courses and challenges
  • Businesses
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

260
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'
}})
over 3 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'
  }
 }
}
over 3 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 Our process Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.

Andres GPT

Show me some job opportunities
There's an error!