javascript - How to use the getIdToken() auth method in firebase version 9? - Stack Overflow

How to use the getIdToken() auth method in firebase version 9?It works like this below in version 8im

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742384551a4433791.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信