Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda