Firebase custom token authentication (firebase version 3)

 ̄綄美尐妖づ 提交于 2019-12-13 16:49:34

问题


I have an authentication server (NodeJS) where I authenticate a user and create a custom firebase token

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

I used to be able to verify a user token (previous version), but now it is not so simple...

I would like to get the decoded userId from the token

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

does not work for server generated custom tokens.

Does anyone know how this could be done?


回答1:


You should be using jsonwebtoken to validate the token in this case. You will just need to pass the firebase private key as an additional parameter.

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.
});

Update:

You have to use the private_key from the .json config file that you set in firebase.initializeApp({ and use a library to convert this key to the public PEM format. You can use node-rsa to do the trick

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.
});


来源:https://stackoverflow.com/questions/38186897/firebase-custom-token-authentication-firebase-version-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!