Firebase Function not returning expected environment configuration when served locally

白昼怎懂夜的黑 提交于 2019-12-24 10:38:49

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/49137279/firebase-function-not-returning-expected-environment-configuration-when-served-l

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!