如何从 Firebase 托管的应用程序中引用 Firebase 函数配置变量? [英] How to reference Firebase Functions config variables from a Firebase-hosted application?

查看:25
本文介绍了如何从 Firebase 托管的应用程序中引用 Firebase 函数配置变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 Heroku 托管的 ReactJS/NodeJS 应用程序转换为托管在 Firebase 上.因为 Firebase 只处理静态页面,所以我需要重新配置用于开发、暂存、生产环境的私有环境特定变量的配置方式.例如在我定义这些类型的变量之前:

I am transitioning a Heroku-hosted ReactJS/NodeJS application to be hosted on Firebase. Because Firebase only handles static pages I need to reconfigure how my private environmental-specific variables for Development, Staging, Production environments are configured. For example before I defined these sorts of variables:

CLIENT_ID=secret_here       
DOMAIN=secret_here    
REDIRECT_URI=secret_here   

在我部署到的 Heroku 环境中,现在我必须从 Firebase CLI 将它们设置为用于开发、暂存和生产的 Firebase 功能环境.

upon the Heroku environment I was deploying to and now I must set them into firebase functions environments from the Firebase CLI for Dev, Staging and Production.

Firebase 有关于向 Firebase 函数添加环境配置的文档,以便我可以在 firebase 函数中添加要在运行时访问的键/值对:

Firebase has documentation on adding environmental configurations to Firebase Functions such that I can add key/value pairs to be accessed at runtime from within a firebase function:

firebase functions:config:set mySecret.key="CLIENT_ID" mySecret.id="secret_here"

但是我不清楚如何从 Firebase 托管的静态应用程序(而不是函数)访问添加的配置变量.

是否像简单地从我的应用程序中引用 firebase 函数库并从我的应用程序中检索定义的密钥一样简单(像这样)?

Is it as simple as simply referencing the firebase functions library from my application and retrieving the defined key from within my application (like so)?

const functions = require('firebase-functions');
...
functions.config().auth0.CLIENT_ID

推荐答案

您通过提供的链接定义的变量只能通过 Cloud Functions 获得.Firebase Hosting 提供的静态内容不能直接使用它们.

The variables you define through the link you provided are only available via Cloud Functions. They're not made available directly to the static content served by Firebase Hosting.

如果你愿意,你可以创建一个 HTTP 函数,它只从你的环境变量返回 JSON,然后从你的 Web 内容中调用它.请记住,您正在向世界公开您的密钥.

If you want, you can make an HTTP function that does nothing but return the JSON from your env vars, and call that from your web content. Bear in mind that you're exposing your secret keys to the world.

这篇关于如何从 Firebase 托管的应用程序中引用 Firebase 函数配置变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆