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

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

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