gpt4 book ai didi

node.js - Firebase 自定义 token 身份验证(Firebase 版本 3)

转载 作者:搜寻专家 更新时间:2023-11-01 00:14:24 25 4
gpt4 key购买 nike

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

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

我以前可以验证一个用户 token (以前的版本),但现在没那么简单了...

我想从 token 中获取解码后的 userId

  firebase.auth().verifyIdToken(token).then(function)....

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

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

最佳答案

在这种情况下,您应该使用 jsonwebtoken 来验证 token 。您只需将 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 并使用一个库来将此 key 转换为 public PEM 格式。您可以使用 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.
});

关于node.js - Firebase 自定义 token 身份验证(Firebase 版本 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38186897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com