简体   繁体   English

Firebase getIdToken 不起作用

[英]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().onAuthStateChangeduser

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.

相关问题 Firebase getIdToken 返回对象 - Firebase getIdToken returns object Firebase .getIdToken() 返回无效令牌 - Firebase .getIdToken() returns invalid token firebase版本9中的getIdToken()认证方法如何使用? - How to use the getIdToken() auth method in firebase version 9? getIdToken()不是函数,即使它像firebase文档中的函数一样使用 - getIdToken() is not a function, even if It is used like a function in firebase document Firebase currentUser.getIdToken() 在 signInWithCustomToken 之后返回自定义令牌 - Firebase currentUser.getIdToken() returns custom token after signInWithCustomToken 在 firebase 中更新密码时出现“TypeError: user.getIdToken is not a function” - Getting "TypeError: user.getIdToken is not a function" when updating password in firebase Firebase:TypeError:无法读取未定义的属性(读取“getIdToken”) - Firebase: TypeError: Cannot read properties of undefined (reading 'getIdToken') 在 ZBF12E1515C251C7D8A1C5Z0352 中实施 session cookies 后无法读取 null 的属性“getIdToken” - Cannot read property 'getIdToken' of null after implementing session cookies in firebase 如何将 firebase jwt getIdToken 发送到 http 获取 ZD18B8624A0F5F721DA7B8235Z 中的请求 - How to send firebase jwt getIdToken to http get request in angular 8? 如何在 reactjs 中发送 firebase getIdToken 和 axios? - How can i send firebase getIdToken with axios in reactjs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM