Wiingaard
Wiingaard

Reputation: 4302

Initialize Admin SDK with credentials depending on environment

I'm new to Node.js, Firebase Cloud Functions and TypeScript.

My goal is to make a cloud function that is a http-endpoint, where the client can authenticate with Firebase. I want the cloud function to return a custom access token if the function succeeds.

I'm using two Firebase project as different environments (development and production). I figured out that I need to initialize the Admin SDK with the ServiceAccountKey.json. My issue is that I have two different ServiceAccountKey-files depending what environment the function is running on.

I've added an configuration variable describing which environment is actively running. I've done so by running: firebase functions:config:set app.environment="production"

Then my plan was to derive the path to the right ServiceAccountKey by checking this configuration.

Here is my entire index.ts file.

import * as functions from 'firebase-functions';

const admin = require('firebase-admin');
const serviceAccountKeyPath = functions.config().app.environment === 'development' ? '../mojnz-development-key.json' : '../mojnz-production-key.json';
const serviceAccount = require(serviceAccountKeyPath);

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://mojnz-dev.firebaseio.com"
});

export const testFunc = functions.https.onRequest((request, response) => {

    const berp = "123"

    admin.auth().createCustomToken(berp).then((value) => {
        console.log("Did create custom token:", value)
        response.sendStatus(200)
    }).catch((error) => {
        console.log("Error creating custom token:", error)
        response.sendStatus(500)
    })
})

I'm looking for answers to:

Why isn't it possible to read the configuration variable at that point?

How is the best practice for initializing modules depending on environment variables?

Edit When I serve the functions locally (like so: firebase serve --only functions). I get error:

i functions: Preparing to emulate functions.

⚠ functions: Failed to load functions source code. Ensure that you have the latest SDK by running npm i --save firebase-functions inside the functions directory.

⚠ functions: Error from emulator. Error occurred while parsing your function triggers.

TypeError: Cannot read property 'environment' of undefined at Object. (/Users/mojnz/Git/mojnz/functions/lib/index.js:7:35) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.require (module.js:497:17) at require (internal/module.js:20:19) at /usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:21:11 at Object. (/usr/local/lib/node_modules/firebase-tools/lib/triggerParser.js:61:3)

I did update the SDK with npm i --save firebase-functions.

Upvotes: 2

Views: 1142

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317467

If you want to use environment variables when testing locally (with serve or functions:shell), there is currently an extra step you have to take to make them show up. It's documented on this page:

If you're using custom functions configuration variables, first run the command to get your custom config (run this within the functions directory), and then run the shell:

firebase functions:config:get > .runtimeconfig.json firebase
functions:shell

Every time you want to change your vars, you'll have to re-create the .runtimeconfig.json file in your functions folder.

Upvotes: 2

Related Questions