1

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"
  },


Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Richard Ortiz
  • 29
  • 1
  • 1
  • 8

1 Answers1

0

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"
});
Richard Ortiz
  • 29
  • 1
  • 1
  • 8