Firebase自定义令牌身份验证(firebase版本3)

Dev*_*138 2 node.js firebase firebase-authentication

我有一个身份验证服务器(NodeJS),我在其中验证用户并创建自定义firebase令牌

   var token = firebase.auth().createCustomToken(userId); 
Run Code Online (Sandbox Code Playgroud)

我曾经能够验证用户令牌(以前的版本),但现在它不是那么简单......

我想从令牌中获取解码的userId

  firebase.auth().verifyIdToken(token).then(function)....
Run Code Online (Sandbox Code Playgroud)

不适用于服务器生成的自定义令牌.

有谁知道如何做到这一点?

ado*_*srs 7

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.
});
Run Code Online (Sandbox Code Playgroud)

更新:

您必须使用您在其中设置private_key.json配置文件firebase.initializeApp({并使用库将此密钥转换为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.
});
Run Code Online (Sandbox Code Playgroud)