Lui*_*Gar 4 javascript web firebase firebase-authentication
我正在使用来自JavaScript客户端的Firebase身份验证进行测试,并且正在尝试使用客户端文档上的检索ID令牌来检索IDToken
我想我忘记了一些基本的知识。
用户已使用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
}
})
Run Code Online (Sandbox Code Playgroud)
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. Added catch and nothing too
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
}
})
Run Code Online (Sandbox Code Playgroud)
小智 5
firebase.auth().user没有user在那一刻呢。您必须像这样直接使用userfrom firebase.auth().onAuthStateChanged:
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
});
}
});
Run Code Online (Sandbox Code Playgroud)
您只能firebase.auth().user在firebase.auth().onAuthStateChanged完成之后使用并且超出其范围,否则它将是未定义的。
| 归档时间: |
|
| 查看次数: |
6906 次 |
| 最近记录: |