'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);


Solution 1:[1]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emre Sülün