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

171
Visualizações
Firebase loses user at refresh

I have a simple login with firebase, but after a refresh it loses the user. The user gets stored in LocalStorage but on refresh it deletes it from there as well.

const loginform = document.querySelector('.loginform');
const email = loginform.email.value;
const password = loginform.password.value;


signInWithEmailAndPassword(auth, email, password)
.then(cred => {
    console.log('user logged in:', cred.user)
    loginform.reset()
    setPersistence(auth, browserLocalPersistence);
})

const auth = getAuth();
const user = auth.currentUser;

onAuthStateChanged(auth, () => {
const userplace = document.querySelector('.user')

if(userplace != null){
    if(user){
        userplace.innerHTML = `Welcome, ${user.displayName || user.email}`    
    }else {
        userplace.innerHTML = `Log in` 
    } 
}
})

and of course since there is no user it will show the "log in" text

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Firebase automatically persists and restores the user credentials on reloading the app/page.

But your code only handles the situation where the user explicitly signs in. To also handle the restore, have a look at the first code snippet in the documentation on getting the current user:

import { getAuth, onAuthStateChanged } from "firebase/auth";

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    // User is signed in, see docs for a list of available properties
    // https://firebase.google.com/docs/reference/js/firebase.User
    const uid = user.uid;
    // ...
  } else {
    // User is signed out
    // ...
  }
});
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