admin管理员组

文章数量:1335584

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

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);
Share Improve this question asked Dec 26, 2021 at 22:07 mannymanny 41511 silver badges24 bronze badges 5
  • getIdToken is still a method on the User object in v9 as shown here firebase.google./docs/reference/js/…. What isn't working about the last code snippet in your question? Did you step through it in a debugger yet? Does currentUser have a value? Does currentUser.uid have a value? – Frank van Puffelen Commented Dec 27, 2021 at 1:02
  • Yes currentUser does have a vaule, how would suggest I use the getIdToken method in v9? @FrankvanPuffelen – manny Commented Dec 27, 2021 at 1:09
  • Same as before, so what happens when you run that last line in the last snippet? – Frank van Puffelen Commented Dec 27, 2021 at 1:12
  • Got this error [Unhandled promise rejection: TypeError: null is not an object (evaluating 'currentUser.getIdToken')] @FrankvanPuffelen – manny Commented Dec 27, 2021 at 1:23
  • 1 That sounds like currentUser is null. If you do if (currentUser === null) throw "No current user" right before calling ID token, that should be easy to test. – Frank van Puffelen Commented Dec 27, 2021 at 1:28
Add a ment  | 

2 Answers 2

Reset to default 3

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.

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)

本文标签: javascriptHow to use the getIdToken() auth method in firebase version 9Stack Overflow