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

207
Visualizações
How to use the getIdToken() auth method in firebase version 9?

How to use the getIdToken() auth method in firebase version 9?

It works like this below in version 8

import firebase from "firebase";

const token = await firebase.auth().currentUser.getIdToken(/* forceRefresh */ false);

I tried this in version 9 but it is not working

import { getIdToken } from "firebase/auth";

const token = await getIdToken(/* forceRefresh */ false);

I also tried this below and it is not working

import { getAuth } from "firebase/auth";

const auth = getAuth();
const { currentUser } = auth;

const token = await currentUser.getIdToken(/* forceRefresh */ false);
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The getIdToken() function takes User as parameter and not the refreshToken boolean as in name-spaced SDK.

import { getIdToken, onAuthStateChanged } from "firebase/auth";

onAuthStateChanged(auth, async (user) => {
  if (user) {
    const token = await getIdToken(user);
  }
});

As far as the error goes, getAuth() returns auth instance but user's auth state might now have loaded yet. Try running the same in onAuthStateChanged or adding a null check as suggested by @Frank.

about 4 years ago · Santiago Trujillo Relatório

0

Looks like getIdToken() function takes two arguments in the latest version (9.6.6): user and a boolean. So, this is the working code in my case:

import { getAuth, getIdToken } from "firebase/auth"

const auth = getAuth()
const { currentUser } = auth
const token = await getIdToken(currentUser, true)
about 4 years ago · Santiago Trujillo 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