Javascript Firebase getToken() TypeError:无法读取属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37934898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:53:13  来源:igfitidea点击:

Firebase getToken() TypeError: Cannot read property

javascriptfirebasefirebase-authentication

提问by thousight

I'm trying to get the token of my currently signed in user of my website. However, javascript cannot get the value for me. I think there are 2 problems here:

我正在尝试获取我网站当前登录用户的令牌。但是,javascript 无法为我获取价值。我认为这里有两个问题:

  1. When I'm getting Auth.currentUser at start, I get this error of "TypeError: Cannot read property 'getToken' of null". But when I type Auth.currentUser.getToken() in the console, the object with the token actually shows up.

  2. What getToken() returns to me is a promise object with its key 'ea' containing the value of the token. but when I do Auth.currentUser.getToken().ea it gets me 'null'. How can I retrieve the token directly from the object?

  1. 当我在开始时获取 Auth.currentUser 时,出现“TypeError:无法读取 null 的属性‘getToken’”的错误。但是当我在控制台中输入 Auth.currentUser.getToken() 时,带有令牌的对象实际上出现了。

  2. getToken() 返回给我的是一个 promise 对象,其键 'ea' 包含令牌的值。但是当我做 Auth.currentUser.getToken().ea 它让我'空'。如何直接从对象中检索令牌?

Thanks!

谢谢!

My code of retrieving the token:

我检索令牌的代码:

var Auth = firebase.auth()
var token = Auth.currentUser.getToken()

This screenshot might be helpful: Chrome console result

此屏幕截图可能会有所帮助: Chrome 控制台结果

回答by Frank van Puffelen

According to the documentation of firebase.User:getIdToken():

根据以下文件firebase.User:getIdToken()

Returns a JWT token used to identify the user to a Firebase service.

Returns the current token if it has not expired, otherwise this will refresh the token and return a new one.

将用于向 Firebase 服务标识用户的 JWT 令牌返回。

如果未过期,则返回当前令牌,否则将刷新令牌并返回一个新令牌。

The method returns a promise, since it may require a round-trip to the Firebase servers in case the token has expired:

该方法返回一个承诺,因为如果令牌已过期,它可能需要往返于 Firebase 服务器:

Auth.currentUser.getIdToken().then(data => console.log(data))

Or in more classic JavaScript:

或者在更经典的 JavaScript 中:

Auth.currentUser.getIdToken().then(function(data) {
    console.log(data)
});

Log output:

日志输出:

ey...biPA

眼睛...双PA

Update: to ensure that the user is signed in before getting the token, run the above code in an onAuthStateChangedlistener:

更新:为确保用户在获取令牌之前已登录,请在onAuthStateChanged侦听器中运行上述代码:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    user.getIdToken().then(function(data) {
      console.log(data)
    });
  }
});

回答by Yang

Here is a sample on how to get the id token using NodeJS

这是一个关于如何使用 NodeJS 获取 id 令牌的示例

var firebase = require('firebase')
firebase.initializeApp({
    apiKey:*********
    databaseURL:*********
})

var customToken = *********    
firebase.auth().signInWithCustomToken(customToken).catch(function(error) {
    var errorMessage = error.message
    console.log(errorMessage)
})

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        firebase.auth().currentUser.getToken().then(data => console.log(data))
    } else {
        console.log('onAuthStateChanged else')
    }
})