5

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..

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
meds
  • 21,699
  • 37
  • 163
  • 314

1 Answers1

4

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • ah, so does this mean if I use the former example you've provided where credential is explicitly passed in my issue should be solved? I did try that method but had the same error pop up.. – meds Dec 17 '17 at 09:34
  • Decoding a JWT should be possible with either approach. The problem is likely elsewhere in your code. As Hiranya commented, enable debug logging to see what it shows. If it remains a problem, I'd recommend opening a separate question with the [minimal code that reproduces that problem](http://stackoverflow.com/help/mcve). – Frank van Puffelen Dec 17 '17 at 15:24
  • Looks like admin.initializeApp(functions.config().firebase) works, when I moved the aws function to be inside cors it suddenly started working. not sure why though. – meds Dec 17 '17 at 18:48