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 theUser
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? DoescurrentUser
have a value? DoescurrentUser.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 thegetIdToken
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
isnull
. If you doif (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
2 Answers
Reset to default 3The 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
版权声明:本文标题:javascript - How to use the getIdToken() auth method in firebase version 9? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742384551a2464765.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论