admin.auth().verifyIdToken(idToken)错误:在8.0.0之后,无法加载firebase



当我尝试在firebase 中使用中间件时遇到了一个问题

这是我的中间件(FBauth(

const db = admin.firestore();
const FBAuth  = (_req,_res,next) => {
let idToken;
if(_req.headers.authorization && _req.headers.authorization.startsWith('Bearer ')){
idToken = _req.headers.authorization.split ('Bearer ')[1];
console.log('ID TOKEN ------------>', idToken);
}else{
console.error('Nessun Token trovato')
return _res.status(403).json({ error: 'non-autorizzato'});
}
admin.auth().verifyIdToken(idToken)
.then(decodedToken => {
_req.user = decodedToken;
console.log(decodedToken);
return db.collection('users')
.where('userId', '==', _req.user.uid)
.limit(1)
.get();
})
.then(data=>{
console.log('****** reading data ******', data);
_req.user.handle = data.doc[0].data().handle;
return next();
})
.catch(err => {
console.error('********error while verifying token*********', err);
return _res.status(403).json({error: `${err}`});
}
);
};

当我尝试验证令牌时,我得到了这个错误

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information

我认为我的代码是正确的引导

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const app =  require('express')();
admin.initializeApp();
const firebaseConfig = {
apiKey: "**************",
authDomain: "***************",
databaseURL: "***************",
projectId: "***************",
storageBucket: "***************",
messagingSenderId: "***************",
appId: "***************",
measurementId: "***************"
};
const firebase = require('firebase');
firebase.initializeApp(firebaseConfig);

可能与此问题相关---->https://github.com/firebase/firebase-admin-node/issues/738?我该怎么解决?

p.S:

我依赖这些

"dependencies": {
"express": "^4.17.1",
"firebase": "^7.13.2",
"firebase-admin": "^8.9.0",
"firebase-functions": "^3.3.0"
},

我可以解决在这里跟随https://stackoverflow.com/a/58140389/6414686

你必须从这里下载文件

你的文件.JSONhttps://console.firebase.google.com/u/0/project/[YOURPROJECTID]/settings/serviceaccounts/adminsdk

var serviceAccount = require("./keys/YOURFILE.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "YOURURLFROMPAGE"
});

相关内容

  • 没有找到相关文章

最新更新