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

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

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 Relatório

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