Reputation: 220
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.
Upvotes: 1
Views: 3416
Reputation: 3184
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
Upvotes: 1