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

143
Visualizações
React-Redux load firebase auth current user in the redux store

I try to persist the redux store state with the currentUser from the Firebase Auth. When I try to access auth.currentUser I get null and I think it's because the currentUser info is loaded asynchronously. What I want is to make my application wait until I fetch the currentUser and then to load the currentUser inside the redux store to access it in my application.

Store.js

import { auth } from "../firebase"
const preloadedState = {
    account: auth.currentUser
}
export const store = createStore(
    reducers,
    preloadedState,
    composeEnhancers(applyMiddleware(thunk)),
)

Firebase.js

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);

How should I wait for currentUser to be fetched and then load inside the redux store to keep the user authenticated?

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

0

According to the documentation, you're supposed to use an auth state observer to get a callback when the user state changes. You can use this information to update your store. (Don't think about this as "waiting" for information - you are respond to changes in state when they happen.)

Attach the observer using the onAuthStateChanged method. When a user successfully signs in, you can get information about the user in the observer.

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