Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!