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

99
Views
getAuth() return a object but uid return null when page reload

My code is so vast; I just put a part of code that I think is essential.

import {getAuth} from "firebase/auth"

const authFirebase = getAuth()
console.log(authFirebase)

const Home = () => {
  ...
  return(
   ...
  )
}

Whenever I reload page (F5) if I just.

console.log(authFirebase)

I retrieved an object that contains uid in the currentUser property, but when I code like this.

 console.log(authFirebase.currentUser.uid)

I got this error

TypeError: Cannot read properties of null (reading 'uid')

I'm so confused; I didn't understand something about Firebase?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

the reason is You’re fetching the user object before that object is ready to be used.

You need to wrap that in a condition if it exists then do log to console.

One more way is to add an observer like onAuthStateChanged this will resolve this problem of prefetching the object.

firebase.auth().onAuthStateChanged( user =>; {
  if (user) { console.log(user) }
});
about 4 years ago · Juan Pablo Isaza Report

0

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!