首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firebase LoginWithCustomToken抛出错误

Firebase LoginWithCustomToken抛出错误
EN

Stack Overflow用户
提问于 2021-08-08 14:19:39
回答 1查看 207关注 0票数 1

我正在使用nodejs中的jsonwebtoken创建firebase自定义令牌,一旦创建了令牌并传递给firebase.auth().signInWithCustomToken(令牌),返回错误消息如下

自定义令牌格式不正确。请查看文档.

Private_key:“--开始私钥

将private_key存储在fb-私有机密. the文件中

Note--以及\n我从防火墙获得的ServiceAccountKey.json文件。

代码语言:javascript
复制
**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.

任何帮助都将不胜感激。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-08-09 00:44:57

若要使用自定义令牌登录,必须使用正确的属性生成。具体地说:

代码语言:javascript
复制
alg: "RS256"
aud: "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit"

更新

Admin解决方案:

代码语言:javascript
复制
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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68701634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档