我正在使用nodejs中的jsonwebtoken创建firebase自定义令牌,一旦创建了令牌并传递给firebase.auth().signInWithCustomToken(令牌),返回错误消息如下
自定义令牌格式不正确。请查看文档.
Private_key:“--开始私钥
将private_key存储在fb-私有机密. the文件中
Note--以及\n我从防火墙获得的ServiceAccountKey.json文件。
**Sample code:-**
const jwt = require("jsonwebtoken");
const fb_private_key = fs.readFileSync('./fb-private-secret.key').toString().replace(/\\n/gm, '\n');
in Nodejs express :-
const token = jwt.sign({
"iss": "https://securetoken.google.com/****",
"sub": user.uid,
"aud": "banded-totality-****",
"iat": new Date().getTime(),
"exp": new Date().getTime()+(60*60*24*5)*1000,
"uid":user.uid
},fb_private_key,{header:{kid:'#####' }, algorithm:'RS256' });
In Reactjs Front-End:-
//token which i get from the server.
const {user} = await firebase.auth().signInWithCustomToken(token);
At the above line (signInWithCustomToken) error comes says
the custom token format is incorrect. please check the documentation.任何帮助都将不胜感激。
谢谢
发布于 2021-08-09 00:44:57
若要使用自定义令牌登录,必须使用正确的属性生成。具体地说:
alg: "RS256"
aud: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit"更新
Admin解决方案:
const admin = require('firebase-admin');
const credentials = require('./credentials.json');
admin.initializeApp({
credential: admin.credential.cert(credentials)
});
// example of claims
const additionalClaims = {
premium: true
}
admin.auth().createCustomToken("user_id_goes_here", additionalClaims)
.then((customToken )=> {
console.log(customToken);
})
.catch(console.error);https://stackoverflow.com/questions/68701634
复制相似问题