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

时间:2016-07-04 14:17:46

标签: node.js firebase firebase-authentication

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

   var token = firebase.auth().createCustomToken(userId); 

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

我想从令牌

中获取已解码的userId
  firebase.auth().verifyIdToken(token).then(function)....

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

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

1 个答案:

答案 0 :(得分: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.
});

更新

您必须使用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.
});