Firebase函数在本地提供时不返回预期的环境配置

问题描述 投票:1回答:1

我在Firebase Hosting上托管了一个React应用程序,它通过调用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端点一样?

node.js reactjs firebase firebase-hosting
1个回答
3
投票

如果要在本地模拟函数中使用配置参数,请使用follow the instructions here。特别:

如果您正在使用自定义函数配置变量,请首先运行该命令以获取您的自定义配置(在函数目录中运行它),然后运行shell:

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

.runtimeconfig.json目录中的functions文件需要包含在仿真期间使用的配置。

© www.soinside.com 2019 - 2024. All rights reserved.