我在Firebase托管上托管了一个React App,它通过调用Firebase函数(an impl recommended here)来检索环境配置值。这是函数的实现:
const functions = require('firebase-functions');
module.exports = functions.https.onRequest((req, res) => {
res.status(200).send(functions.config());
});
来自Firebase CLI,当我致电:
firebase functions:config:get
我明白了:
{
"auth": {
"clientid": MY_CLIENT_ID,
"signoutreturnto": SOME_URL,
"responsetype": SOME_URL,
"redirecturi": SOME_OTHER_URL,
"scope": SOME_OTHER_STRING,
"domain": SOME_DOMAIN
}
}
从Firebase CLI调用“Firebase deploy --only functions”会产生一个URL端点,用于调用已部署的firebase函数config,该函数将返回我的环境配置:
https://us-central1-MY_FIREBASE_APP.cloudfunctions.net/config
调用此端点将返回表示我的配置+ Firebase附加的预期JSON:
{
"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_": {}
}
}
}
我有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"
}
]
}
}
但是 - 当我使用Firebase CLI中的以下命令在本地提供函数时:
Firebase serve --only functions,hosting
我为配置函数调用生成的本地端点:
http://localhost:5001/MY_FIREBASE_APP/us-central1/config
返回的json对象缺少预期的“auth”对象(如上所示)。相反,返回的JSON是:
{
"firebase": {
"databaseURL": MY_DATABASE_URL,
"storageBucket": MY_STORAGE_BUCKET,
"apiKey": MY_API_KEY,
"authDomain": MY_AUTH_DOMAIN,
"projectId": MY_PROJECT_ID,
"credential": {
"credential_": {
...
}
}
}
}
}
这会在本地执行时中断我的应用程序,因为无法检索到预期的配置值。
为什么我配置的“auth”对象不包含在结果中,就像我调用其他url端点一样?
答案 0 :(得分:3)
如果要在本地模拟的函数中使用配置参数follow the instructions here。具体做法是:
如果您正在使用自定义函数配置变量,请先运行命令以获取自定义配置(在函数目录中运行),然后运行shell:
cd functions firebase functions:config:get > .runtimeconfig.json
.runtimeconfig.json
目录中的functions
文件需要包含在仿真期间使用的配置。