我正在使用来自 javascript 客户端的 Firebase 身份验证进行测试,我正在尝试 通过客户端文档上的检索 id 令牌检索 idToken
我想我忘记了一些基本的东西。
用户使用 Google 登录
该代码只是我在其他帖子和文档中看到的。结果在评论中。
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
}
})
谢谢
编辑:
如果我添加任何错误,也不会发生任何事情。例如,如果我添加一个警报,它会显示警报,但如果我有错误,例如 alter() 不会显示任何错误。添加了捕获,什么也没有
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
}
})
原文由 Luis Gar 发布,翻译遵循 CC BY-SA 4.0 许可协议
firebase.auth().user
在那一刻还没有user
。你必须使用user
来自firebase.auth().onAuthStateChanged
直接像这样:您只能在
firebase.auth().onAuthStateChanged
firebase.auth().user
,否则它将未定义。