Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

305
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda