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
How do I prevent firebase accessToken from expiring after 1 hour?

I am building an App using firebase authentication. I pass in the firebase token to the API auth headers to validate the API.

The issue is after 1 hour the token expires and the API does not work anymore? How can I refresh the auth token every time it expires so that essentially the token never expires when the user is logged in.

I am using a auth context

here is AuthContext.jsx

  function getRefreshToken() {
    auth.onIdTokenChanged(async (user) => {
      await user?.getIdToken().then(async (token) => token);
    });
  }

  useEffect(() => {
    // setInterval(() => {
    // auth.onIdTokenChanged(async (user) => {
    //   await user?.getIdToken(true).then((token) => {
    //     setCurrentUser({
    //       ...currentUser,
    //       user: user || null,
    //       isVerified: user ? user.emailVerified : false,
    //       email: user ? user.email : '',
    //       accessToken: user ? token : '',
    //     });
    //     console.log(token, 'token');
    //   });
    // });

    // logout();
    // }, 3300000); // every 55 minutes set the access token to the new idToken

    const unsubscribe = auth.onAuthStateChanged((user) => {
      setCurrentUser({
        ...currentUser,
        user: user || null,
        isVerified: user ? user.emailVerified : false,
        email: user ? user.email : '',
        accessToken: user ? user.Aa : '',
      });
      setLoading(false);
    });
    return unsubscribe;
  }, []);

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

0

You'll need to manually refresh the token when it expires.

Store the token's expiry time somewhere. When you make an API call (assuming you're using axios), you can use a request interceptor to add the token. Just check if its expired first - if it is, fetch a new one using the refresh token and then make the API call. If it is not, just send the request.

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