meds
meds

Reputation: 22956

How do I include my firebase service account key when using functions.config().firebase to init?

I'm initializing my firebase functions like so:

admin.initializeApp(functions.config().firebase)

I've generated a service account key which I believe I need to do for auth purpouses.

It gave me a json table with various key/values.

The instructions were to add that in admin.initializeApp like so:

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

Which is very different to how I'md oing it.

I'm not even sure I need to do this though because I do have access to firestore using my previous method, however auth with valid user id tokens is not working giving me the following error in firebase:

ERROR: Error: Decoding Firebase ID token failed. Make sure you passed the entire string JWT which represents an ID token. See https://firebase.google.com/docs/auth/admin/verify-id-tokens for details on how to retrieve an ID token.

and from sniffing around it looked like the missing thing was the admin sdk service account..

Upvotes: 6

Views: 5101

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599601

This snippet is a general way to initialize the Firebase Admin SDK for Node.js:

var serviceAccount = require("path/to/serviceAccountKey.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

As you've seen, it requires that you download a JSON file from the console, and add it to the path.

This is a bit finicky, and some developers find it hard to get working. Since the Cloud Functions environment is fully under Firebase's control, it was made a bit easier there. Your other snippet shows how:

admin.initializeApp()

Both snippets accomplish the same thing, but the latter only works in Cloud Functions for Firebase.

Upvotes: 5

Related Questions