Reputation: 111
Many functions have started to fail intermittently with the following error even before the first line of code is executed:
FIREBASE WARNING: {"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: "Failed to parse access token response: SyntaxError: Unexpected token p in JSON at position 4"."}
It's been more than 5 five days since i had opened a bug issue with Firebase support and i still don't have any feedback.
Does anyone why this is happening and how it can be fixed?
Libraries used and their versions:
"firebase": "^8.2.9",
"firebase-admin": "^9.5.0",
"firebase-functions": "^3.1.0"
Initializations:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
var config = {
apiKey: "...",
authDomain: "...",
databaseURL: "..."
};
var firebase = require("firebase");
firebase.initializeApp(config);
Upvotes: 0
Views: 449
Reputation: 83093
In a Cloud Function, if you want to interact with the Firebase services (e.g. Firestore, Auth service, etc), you need to use the Admin SDK.
So, you need to load the firebase-functions
and firebase-admin
modules, and initialize an admin
app instance from which you interact with the services, as follows:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const firestoreDB = admin.firestore();
const authService = admin.auth();
// ...
// Examples:
// In a Cloud Function
return firestoreDB.collection("cities").doc("LA").set({
name: "Los Angeles",
state: "CA",
country: "USA"
});
// In another Cloud Function
return authService.updateUser(uid, {
email: '[email protected]',
phoneNumber: '+11234567890',
})
.then((userRecord) => {
// ...
// return ...
})
In other words, you don't need to do:
var config = {
apiKey: "...",
authDomain: "...",
databaseURL: "..."
};
var firebase = require("firebase");
firebase.initializeApp(config);
Also note this note from the doc:
In many cases, new features and bug fixes are available only with the latest version of the Firebase CLI and the
firebase-functions
SDK. It's a good practice to frequently update both the Firebase CLI and the SDK with these commands inside the functions folder of your Firebase project:
npm install firebase-functions@latest firebase-admin@latest --save
npm install -g firebase-tools
Upvotes: 2