[英]Firebase getIdToken not working
I'm makeing tests with Firebase Authentication from javascript client and I'm trying to retrieve the idToken with retrieve id tokens on clients documentation我正在使用来自 javascript 客户端的 Firebase 身份验证进行测试,并且我正在尝试使用客户端文档中的检索 id 令牌检索 idToken
I think I'm forgetting something basic.我想我忘记了一些基本的东西。
A user is logged in with Google用户使用 Google 登录
The code is just i've seen in other posts and in the documentation.代码只是我在其他帖子和文档中看到的。 And the result is in the comments.
结果在评论中。
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log(user); // this is shown. Firebase user and provider data
console.log(user.uid); // Shown
firebase.auth().user.getIdToken().then(function(idToken) {
console.log(idToken+'--'); // Nothing happens. No errors and the function not continues
});
console.log(user.uid); // Nothing happens
}
})
Thanks谢谢
EDIT:编辑:
if I add anything wrong nothing happens too.如果我添加任何错误,也不会发生任何事情。 for example if I add an alert it shows the alert but if I have a mistake, for example alter() not shows any error.
例如,如果我添加警报,它会显示警报,但如果我有错误,例如 alter() 不会显示任何错误。 Added catch and nothing too
添加了 catch ,什么也没有
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
alter() // Nothing happens and the function stop
console.log(user); // this is shown. Firebase user and provider data
console.log(user.uid); // Shown
firebase.auth().user.getIdToken().then(function(idToken) {
console.log(idToken+'--'); // Nothing happens. No errors and the function not continues
}).catch(function(error) {
console.log(error+'--'); // Nothing
});
console.log(user.uid); // Nothing happens
}
})
firebase.auth().user
doesn't have the user
in that moment yet. firebase.auth().user
在那一刻还没有user
。 You have to use the user
from firebase.auth().onAuthStateChanged
directly like this:您必须像这样直接使用来自
firebase.auth().onAuthStateChanged
的user
:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
console.log(user); // It shows the Firebase user
console.log(firebase.auth().user); // It is still undefined
user.getIdToken().then(function(idToken) { // <------ Check this line
console.log(idToken); // It shows the Firebase token now
});
}
});
You can only use firebase.auth().user
after firebase.auth().onAuthStateChanged
is completed and outside of its scope, otherwise it will be undefined.您只能在
firebase.auth().onAuthStateChanged
完成并且超出其范围后使用firebase.auth().user
,否则它将是未定义的。
firebase.auth().currentUser is synchronous. firebase.auth().currentUser 是同步的。 We can make it asynchronous by subscribing to the auth observable instead.
我们可以通过订阅 auth observable 来使其异步。
Depending on the library we're using, the JavaScript SDK has onAuthStateChanged() and AngularFire2 has both authState() onAuthStateChanged().根据我们使用的库,JavaScript SDK 有 onAuthStateChanged(),AngularFire2 有 authState() onAuthStateChanged()。
// For AngularFire:
private afAuth: AngularFireAuth,
afAuth.authState.subscribe(user => {
if (user) {
user.getIdToken(true).then(idToken => {
// ...
});
}
});
// or
this.afAuth.auth.onAuthStateChanged(user => {
if (user) {
user.getIdToken(true).then(idToken => {
//...
});
}
});
// For the JS SDK
firebase.auth().onAuthStateChanged(user => {
if (user) {
user.getIdToken(true).then(idToken => {
// ...
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.