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

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

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 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