gpt4 book ai didi

javascript - 如何使用 JWT token 对网站用户进行 FireBase 身份验证?

转载 作者:行者123 更新时间:2023-12-05 07:32:47 25 4
gpt4 key购买 nike

我正在构建一个使用 Firebase 的移动应用程序。登录时,我希望应用程序从我的网站数据库对用户进行身份验证,然后将该信息复制到 firebase 数据库。创建帐户后,它应该写入网站数据库,然后写入 firebase 数据库。

现在我感觉有点陷入困境,我不确定我是否在朝着正确的方向前进,或者我是否应该完全重新开始并从另一个 Angular 来处理这个问题。现在以下错误困扰着我,我不知道这个 key 应该来自哪里:

undefined
/codestudio/firebasesdk.js:40
var signingKey = key.publicKey || key.rsaPublicKey;
^

TypeError: Cannot read property 'publicKey' of undefined
at /codestudio/firebasesdk.js:40:26
at /codestudio/node_modules/lru-memoizer/index.js:81:20
at Array.forEach (<anonymous>)
at /codestudio/node_modules/lru-memoizer/index.js:77:17
at /codestudio/node_modules/jwks-rsa/lib/wrappers/cache.js:22:18
at /codestudio/node_modules/jwks-rsa/lib/JwksClient.js:51:18
at /codestudio/node_modules/jwks-rsa/lib/JwksClient.js:104:18
at Request._callback (/home/ethreil/Desktop/codestudio/node_modules/jwks-rsa/lib/JwksClient.js:88:20)
at Request.self.callback (/home/ethreil/Desktop/codestudio/node_modules/request/request.js:185:22)

我的代码

var admin = require('firebase-admin');
var refreshToken; // Get refresh token from OAuth2 flow

var serviceAccount = require('/home/ethreil/Desktop/codestudio/ggbsdk.json');
var uid = "some-uid";


admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://goodgamebuzz.firebaseio.com'

});

var fs = require('fs');
var cert = fs.readFileSync('/home/ethreil/Desktop/codestudio/privateKey.pem');
var jwt = require('jsonwebtoken');

var token = jwt.sign({
expiresIn: 36000 ,
audience: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit" ,
issuer: 'myserviceaccount@gmail.com' ,
subject: 'myservicecaccount@gmail.com' ,
iat: Math.floor(Date.now())
} ,
cert
,
{
algorithm: 'RS256' ,
keyid: '7a3d1904f18e257948831eab803e12b7971e13ab'
});

const kid = '7a3d1904f18e257948831eab803e12b7971e13ab';
var jwksClient = require('jwks-rsa');
var client = jwksClient({
cache:true ,
jwksUri: 'https://goodgamebuzz.com/wp-json/jwt-auth/v1/token/validate'
});

client.getSigningKey(kid, function(err, key) {
var signingKey = key.publicKey || key.rsaPublicKey;
callback(null, signingKey);
});


jwt.verify(token, cert, {
algorithms: 'RS256' ,
audience: 'goodgamebuzz' ,
issuer: "https://securetoken.google.com/goodgamebuzz" ,
subject: uid

} ,
function(err, decoded) {
console.log(decoded) // bar
});

最佳答案

您是否尝试过在获取签名 key 时不使用回调?

根据文档 https://www.npmjs.com/package/jwks-rsa :

const kid = '<some kid>';
const key = await client.getSigningKey(kid);
const signingKey = key.getPublicKey();

关于javascript - 如何使用 JWT token 对网站用户进行 FireBase 身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51051557/

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