1
votes

I was trying to use firebase cloud functions and firebase-admin combination to make a route for adding an auth claim to my users in auth. I was testing it with firebase serve and I tried below code for doing so:

const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);

const func = () => {
    admin.auth().setCustomUserClaims("(req.params.uid", {isActivated: true}).then(() => {
        console.log("Done")
        })
        .catch(e => console.error(e))
}
setTimeout(func, 4000)


// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.api = functions.https.onRequest((request, response) => {
console.log(functions.config())
 response.send("Hello from Firebase!");
});

And that didn't worked actually and gives me below error:

{ Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND".
>      at FirebaseAppError.FirebaseError [as constructor] (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:42:28)
>      at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:88:28)
>      at new FirebaseAppError (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\utils\error.js:123:28)
>      at C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\firebase-admin\lib\firebase-app.js:121:23
>      at process._tickCallback (internal/process/next_tick.js:68:7)
>    errorInfo:
>     { code: 'app/invalid-credential',
>       message:
>        'Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following 
error: "Error fetching access token: Error while making request: getaddrinfo ENOTFOUND metadata.google.internal metadata.google.internal:80. Error code: ENOTFOUND".' },
>    codePrefix: 'app' }

And one blog post was saying that if I want to use anything other than firestore and realtime database with firebase-admin I have to do some extra configurations so I tried testing whether firestore works fine or not by using below code:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();

exports.api = functions.https.onRequest((req, res) => {
    var db = admin.firestore();
    db.collection('users').get()
    .then(snapshot => {
        snapshot.forEach(doc => {
            console.log(doc.id, '=>', doc.data());
        });
    })
    .catch(err => {
        console.log('Error getting documents', err);
    }); 
  res.send("Hi")
});

again it gave me another error related to the admin credentials itself:

Error getting documents Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
>      at GoogleAuth.getApplicationDefaultAsync (C:\Users\jishn\WorkSpace\Works\Status\firebase\functions\node_modules\google-auth-library\build\src\auth\googleauth.js:159:19)
>      at process._tickCallback (internal/process/next_tick.js:68:7)

and functions.config() returns {}

Whatever happened is happed on the command firebase serve. And below code works with a serviceAccount.json file:

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://status-kgi.firebaseio.com"
});

But having to use this kind of admin initialization for cloud functions is not a good idea I think.

1
Please edit the question to be very specific about what you are doing to get this error. Are you running locally in the firebase emulator? - Doug Stevenson
@DougStevenson I was running firebase serve on my laptop - Jishnu Raj
Just ran *firebase init selected cloud functions and hosting, then added the above code to the index.js file and added .api function to firebase.json - Jishnu Raj
This is a common problem. If you do a search for that error message, you will find a LOT of information. Please file an issue with firebase-tools if you don't see an issue that addresses the problem. github.com/firebase/firebase-tools/… - Doug Stevenson
@DougStevenson already tried searching a lot, but no results were found for the specific case. There is no huge code in this file at all. just the module imports, admin initialization and a http trigger. thats all. I expect my small test code doesn't have any error in it. - Jishnu Raj

1 Answers

0
votes

From Github issue resolution:

Looking through some old issues, I believe this was fixed in version 8.5.0 by #2214

Solution as follows:

const admin = require('firebase-admin');
admin.initializeApp(); // no neeed for functions.config().firebase