6

I need to access firebase admin through Firebase functions. I have a function that is being called through an HTTP trigger, and will create custom auth tokens.

In the Firebase admin documentation, they say that you need to refer to the JSON file containing all the keys (serviceAccount)

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

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

Since i'm already inside my own Firebase functions, will i need to upload these keys or can I access this in any other way?

It feels very unnecessary uploading all my admin keys to Firebase storage just to mint new tokens...

Giovanni Palusa
  • 1,197
  • 1
  • 16
  • 36

1 Answers1

13

You can use functions.config().firebase to obtain the Firebase config instance and then pass this directly to the initializeApp() method of the Admin SDK:

var admin = require("firebase-admin");
var functions = require("firebase-functions");

// Pass the Firebase config directly to initializeApp() to auto-configure
// the Admin Node.js SDK.
admin.initializeApp(functions.config().firebase);

From the use environment configuration to initialize a module documentation:

When you deploy functions using the Firebase CLI, functions.config().firebase is auto-populated with configuration needed to initialize the firebase-admin SDK.

So you can put this in your code:

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

It has been noted that functions.config() is only available within the Cloud Functions hosted environment and therefore not available when emulating functions locally. As a workaround, you can export functions.config() to .runtimeconfig.json by running the following command inside your functions directory:

firebase functions:config:get > .runtimeconfig.json
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
  • 1
    Thanks! This was exactly what I was looking for – Giovanni Palusa Nov 06 '17 at 10:53
  • 1
    great, but it wont work when serving functions locally using functions-emulator. – Qasim Jan 23 '19 at 11:44
  • @Qasim See [this answer for another question](https://stackoverflow.com/a/42978220/2754146) which points to [serve HTTP functions from the command line documentation](https://firebase.google.com/docs/functions/local-emulator#serve_http_functions_from_the_command_line). Specifically try running: `firebase functions:config:get > .runtimeconfig.json` in your functions directory. – Grimthorr Jan 23 '19 at 11:53
  • @Grimthorr yup, that's what i'm using for local env, thanks – Qasim Jan 23 '19 at 12:04
  • This is not working for me, i got the next error: Error updating user: { 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 – NaturalDevCR Oct 23 '20 at 18:39