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

199
Visualizações
Is getting currently authenticated user an asynchronous call?

If I get currently authenticated user through auth.currentUser, is this an asynchronous cal and be handled as such?

Namely, I have this top-level App component in react with firebase on the backend.

import { BrowserRouter, Routes, Route } from 'react-router-dom';

import MainNav from './layouts/Navbar.js';
import Homepage from './pages/Homepage';
import Login from './pages/Login';
import Register from './pages/Register';
import Profile from './pages/Profile';
import Question from './pages/Question';
import auth from 'auth/path/from/firebase-config'

function App() {

  const user = auth.currentUser
  
  return (
    <>
      <BrowserRouter>
        <MainNav />
        <Routes>
          <Route path="/" element={<Homepage />} />
          <Route path="/questions/:id" element={<Question />} />
          <Route path="/users/:id" element={<Profile />} />
          <Route path="/login" element={<Login />} />
          <Route path="/register" element={<Register />} />
        </Routes>
      </BrowserRouter>
    </>
  );
}

export default App;

I would like to know if there is an authenticated user and tell that to the . Depending on whether there is a user I want to show register, and login buttons or logout button (or something like that)

Is there maybe a way here to utilize firebase's onAuthStateChanged observer?

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

0

Firebase automatically restores the user credentials when the page/app reloads. This requires it to make a call to the server, so happens asynchronously. This call likely hasn't completed when your auth.currentUser runs, which means you get null for the current user.

The solution is indeed as you say to use an auth state listener, which fires for the first time after the asynchronous call has compelte.

about 4 years ago · Juan Pablo Isaza Relatório

0

Yes, you must wait onAuthStateChanged to get it ready. In my app, I created a top-level component to handle this case. This component blocks render, until firebase get ready.

import { useCallback, useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { fetchProfileDetails, handleAuthStateChanged } from '../../redux/actions/auth';
import { getAuth, onAuthStateChanged } from 'firebase/auth'

const auth = getAuth()

export default function AuthGate({ children }) {
  const dispatch = useDispatch()
  const authStatus = useSelector(state => state.auth.status) //idle (default, onAuthStateChanged not yet fired) || authenticated || unauthenticated
  const profileStatus = useSelector(state => state.auth.profile.query.status) //idle || loading || succeeded || failed.

  useEffect(() => {
    const unsubscribe = onAuthStateChanged(auth, user => {
      dispatch(handleAuthStateChanged(user)) // this line is responsible to change authStatus from `idle` to `authenticated` || `unauthenticated`
    })
    return () => {
      unsubscribe()
    }
  }, [dispatch]);

  useEffect(() => {
    if (authStatus === 'authenticated') {
      dispatch(fetchProfileDetails()) //fetch profile details from backend each user authenticated
    } else {
      // clear profile details each logout
    }
  }, [authStatus, dispatch]);


  const renderChildren = useCallback(() => {
    if (authStatus === 'idle' || profileStatus === 'idle' || profileStatus === 'loading') {
      //show loading component
    } else if (profileStatus === 'failed') {
      //show error component
    } else {
      return children
    }
  }, [authStatus, children, profileStatus])

  return renderChildren()
}

index.js

//...
ReactDOM.render(
  //...
    <AuthGate>
      <App />
    </AuthGate>
  //...
  ,
  document.getElementById('root')
);
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