Firebase getToken() TypeError: Cannot read property
Firebase getToken() TypeError: Cannot read property
我正在尝试获取我网站当前登录用户的令牌。但是,javascript 无法为我获取值。我认为这里有两个问题:
当我开始 Auth.currentUser 时,我得到 "TypeError: Cannot read property 'getToken' of null" 这个错误。但是当我在控制台中键入 Auth.currentUser.getToken() 时,带有令牌的对象实际上出现了。
getToken() returns 对我来说是一个承诺对象,其键 'ea' 包含令牌的值。但是当我做 Auth.currentUser.getToken().ea 时,它让我 'null'。如何直接从对象中检索令牌?
谢谢!
我的取币代码:
var Auth = firebase.auth()
var token = Auth.currentUser.getToken()
此屏幕截图可能会有所帮助:
根据 documentation of 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.
方法 returns 一个承诺,因为它可能需要往返 Firebase 服务器以防令牌过期:
Auth.currentUser.getIdToken().then(data => console.log(data))
或者更经典的JavaScript:
Auth.currentUser.getIdToken().then(function(data) {
console.log(data)
});
日志输出:
ey...biPA
更新:为了确保用户在获取令牌之前已经登录,运行上面的代码在onAuthStateChanged
listener:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
user.getIdToken().then(function(data) {
console.log(data)
});
}
});
这是一个关于如何使用 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')
}
})
我正在尝试获取我网站当前登录用户的令牌。但是,javascript 无法为我获取值。我认为这里有两个问题:
当我开始 Auth.currentUser 时,我得到 "TypeError: Cannot read property 'getToken' of null" 这个错误。但是当我在控制台中键入 Auth.currentUser.getToken() 时,带有令牌的对象实际上出现了。
getToken() returns 对我来说是一个承诺对象,其键 'ea' 包含令牌的值。但是当我做 Auth.currentUser.getToken().ea 时,它让我 'null'。如何直接从对象中检索令牌?
谢谢!
我的取币代码:
var Auth = firebase.auth()
var token = Auth.currentUser.getToken()
此屏幕截图可能会有所帮助:
根据 documentation of 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.
方法 returns 一个承诺,因为它可能需要往返 Firebase 服务器以防令牌过期:
Auth.currentUser.getIdToken().then(data => console.log(data))
或者更经典的JavaScript:
Auth.currentUser.getIdToken().then(function(data) {
console.log(data)
});
日志输出:
ey...biPA
更新:为了确保用户在获取令牌之前已经登录,运行上面的代码在onAuthStateChanged
listener:
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
user.getIdToken().then(function(data) {
console.log(data)
});
}
});
这是一个关于如何使用 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')
}
})