1

I have a React App hosted on Firebase Hosting that retrieves environment configuration values via a call to a Firebase Function (an impl recommended here). Here is the implementation of the function:

const functions = require('firebase-functions');

module.exports = functions.https.onRequest((req, res) => {
    res.status(200).send(functions.config());
});

From the Firebase CLI, when I call:

firebase functions:config:get

I see:

{
  "auth": {
        "clientid": MY_CLIENT_ID,
        "signoutreturnto": SOME_URL,
        "responsetype": SOME_URL,
        "redirecturi": SOME_OTHER_URL,
        "scope": SOME_OTHER_STRING,
        "domain": SOME_DOMAIN
    }
}

calling "Firebase deploy --only functions" from the Firebase CLI yields a URL endpoint to invoke the deployed firebase function, config, which returns my environmental config:

https://us-central1-MY_FIREBASE_APP.cloudfunctions.net/config

Invoking this endpoint returns the expected JSON representing my configuration + that appended by Firebase:

{
    "auth": {
        "clientid": MY_CLIENT_ID,
        "signoutreturnto": SOME_URL,
        "responsetype": SOME_URL,
        "redirecturi": SOME_OTHER_URL,
        "scope": SOME_OTHER_STRING,
        "domain": SOME_DOMAIN
    },
    "firebase": {
        "databaseURL": MY_DATABASE_URL,
        "storageBucket": MY_STORAGE_BUCKET,
        "apiKey": MY_API_KEY,
        "authDomain": MY_AUTH_DOMAIN,
        "projectId": MY_PROJECT_ID,
        "credential": {
            "credential_": {}
        }
    }
}

I have edited my firebase.json with re-write rules to serve my functions as API endpoints

{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      },
      {  
        "source": "/config", "function": "config"
      }
    ]
  }
}

However - when I serve the functions locally using the following command from the Firebase CLI:

Firebase serve --only functions,hosting

and I invoke the generated, local endpoint for my config function:

http://localhost:5001/MY_FIREBASE_APP/us-central1/config

The returned json object is missing the expected "auth" object (seen above). Instead, the JSON returned is:

{
    "firebase": {
        "databaseURL": MY_DATABASE_URL,
        "storageBucket": MY_STORAGE_BUCKET,
        "apiKey": MY_API_KEY,
        "authDomain": MY_AUTH_DOMAIN,
        "projectId": MY_PROJECT_ID,
        "credential": {
            "credential_": {
                  ...
                }
            }
        }
    }
}

This breaks my application while executing locally as expected configuration values cannot be retrieved.

Why is my configured "auth" object not contained in the result like it is when I invoke the other url endpoint?

TheFastCat
  • 3,134
  • 4
  • 22
  • 32

1 Answers1

3

If you want to use configuration parameters in locally emulated functions, follow the instructions here. Specifically:

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:

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

That .runtimeconfig.json file in the functions directory needs to contain your config to be used during emulation.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • Thank you for the link, Doug. I created another issue in SO for the error I encounter following the directions : https://stackoverflow.com/questions/49139823/error-invoking-firebase-function-referencing-local-configuration-variables-from – TheFastCat Mar 06 '18 at 20:58
  • @Doug Stevenson - Just a heads up, but the docs "hide" the instructions for custom configuration/environment under the shell instructions. As it stands now, it is easy to overlook if you're using the `serve --only functions` approach. – MandisaW Jul 02 '18 at 21:45
  • 1
    @MandisaW You can always send your feedback directly to the doc authors by using the "send feedback" button in the upper right of any page. – Doug Stevenson Jul 02 '18 at 21:56
  • @DougStevenson And done - I suppose I overlooked that as well LOL – MandisaW Jul 03 '18 at 15:04