Firebase 自定义令牌身份验证(Firebase 版本 3)
Firebase custom token authentication (firebase version 3)
我有一个身份验证服务器 (NodeJS),我在其中对用户进行身份验证并创建自定义 firebase 令牌
var token = firebase.auth().createCustomToken(userId);
我以前可以验证用户令牌(以前的版本),但现在没那么简单了...
我想从令牌中获取解码后的 userId
firebase.auth().verifyIdToken(token).then(function)....
不适用于服务器生成的自定义令牌。
有谁知道如何做到这一点?
在这种情况下,您应该使用 jsonwebtoken
来验证令牌。您只需要将 firebase 私钥作为附加参数传递。
var jwt = require('jsonwebtoken');
var fbPrivateKey = //your firebase key string here
jwt.verify(token, fbPrivateKey, { algorithms: ['RS256'] }, function(err, decoded) {
console.log(decoded); //your token info will be available here.
});
更新:
您必须使用您在 firebase.initializeApp({
中设置的 .json
配置文件中的 private_key
,并使用库将此密钥转换为 public PEM format
。你可以使用 node-rsa 来完成这个技巧
var NodeRSA = require('node-rsa');
var fbPrivateKey = //key from the .json file.
var key = new NodeRSA(fbPrivateKey).exportKey('pkcs8-public-pem');
jwt.verify(token, key, { algorithms: ['RS256'] }, function(err, decoded) {
console.log(err);
console.log(decoded); //your token info will be available here.
});
我有一个身份验证服务器 (NodeJS),我在其中对用户进行身份验证并创建自定义 firebase 令牌
var token = firebase.auth().createCustomToken(userId);
我以前可以验证用户令牌(以前的版本),但现在没那么简单了...
我想从令牌中获取解码后的 userId
firebase.auth().verifyIdToken(token).then(function)....
不适用于服务器生成的自定义令牌。
有谁知道如何做到这一点?
在这种情况下,您应该使用 jsonwebtoken
来验证令牌。您只需要将 firebase 私钥作为附加参数传递。
var jwt = require('jsonwebtoken');
var fbPrivateKey = //your firebase key string here
jwt.verify(token, fbPrivateKey, { algorithms: ['RS256'] }, function(err, decoded) {
console.log(decoded); //your token info will be available here.
});
更新:
您必须使用您在 firebase.initializeApp({
中设置的 .json
配置文件中的 private_key
,并使用库将此密钥转换为 public PEM format
。你可以使用 node-rsa 来完成这个技巧
var NodeRSA = require('node-rsa');
var fbPrivateKey = //key from the .json file.
var key = new NodeRSA(fbPrivateKey).exportKey('pkcs8-public-pem');
jwt.verify(token, key, { algorithms: ['RS256'] }, function(err, decoded) {
console.log(err);
console.log(decoded); //your token info will be available here.
});