admin.auth().verifyIdToken(idToken) Error: Could not load the default credentials whith firebase-admin after to 8.0.0

一笑奈何 提交于 2021-01-29 06:01:35

问题


I've got a problem when i'm trying use middleware in firebase

this is my middleware (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}`});
        }
      );
};

When i try verify token I've got this error

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

I think my code is correctly bootstrap

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

maybe is correlated whit this ISSUE ----> https://github.com/firebase/firebase-admin-node/issues/738 ? How i can solve?

P.S:

my depencies are these

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



回答1:


I can solve following here https://stackoverflow.com/a/58140389/6414686

you must download file from here

YOURFILE.JSON https://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"
});


来源:https://stackoverflow.com/questions/61161388/admin-auth-verifyidtokenidtoken-error-could-not-load-the-default-credential

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