简体   繁体   English

将 firebase 配置存储在 .env 文件中

[英]Storing firebase config in .env files

I have a Sveltekit project where I am using firebase for authentication.我有一个 Sveltekit 项目,我在其中使用 firebase 进行身份验证。 I am storing the firebase.js file which contains the config parameters like apiKey and authDomain in the \src\lib folder.我将包含 apiKey 和 authDomain 等配置参数的 firebase.js 文件存储在 \src\lib 文件夹中。 I am storing the apiKey value in the .env file which is present at the root of the project and using the process.env.API_KEY to load the value for the same.我将 apiKey 值存储在项目根目录中的 .env 文件中,并使用 process.env.API_KEY 加载相同的值。 However when I do npm run build I get an error in the browser console - "process is not defined".但是,当我执行 npm run build 时,我在浏览器控制台中收到错误 - “未定义进程”。 I tried the below approaches but none of them seem to work -我尝试了以下方法,但它们似乎都不起作用 -

Approach 1 -方法 1 -

import { initializeApp } from 'firebase/app'
import { getAuth } from 'firebase/auth'
import { config } from 'dotenv'

config()

const firebaseConfig = {
  apiKey: process.env.API_KEY,
  authDomain: process.env.AUTH_DOMAIN,
}

const app = initializeApp(firebaseConfig)
const auth = getAuth(app)

export default auth

Approach 2 -方法 2 -

Here I used the env-cmd library which I found via another Stackoverflow post.在这里,我使用了通过另一个 Stackoverflow 帖子找到的 env-cmd 库。 Below is the updated code下面是更新的代码

import { initializeApp } from 'firebase/app'
import { getAuth } from 'firebase/auth'

const firebaseConfig = {
  apiKey: process.env['API_KEY'],
  authDomain: process.env['AUTH_DOMAIN'],
}

const app = initializeApp(firebaseConfig)
const auth = getAuth(app)

export default auth

I also updated the scripts in package.json like below -我还更新了 package.json 中的脚本,如下所示 -

"scripts": {
        "dev": "env-cmd vite dev",
        "build": "env-cmd vite build",
        "package": "svelte-kit package",
        "preview": "vite preview",
        "prepare": "svelte-kit sync"
    }

Neither of the approaches seem to work and I still get the same error ie "process is not defined".这两种方法似乎都不起作用,我仍然得到相同的错误,即“未定义进程”。 Even some of the other repos I checked on Github had the apiKey hard coded in the config file, which obviously is a bad practice, even for hobby projects.甚至我在 Github 上查看的其他一些存储库也将 apiKey 硬编码在配置文件中,这显然是一种不好的做法,即使对于业余项目也是如此。

Any ideas on how I could incorporate the firebase apiKey via the .env file?关于如何通过 .env 文件合并 firebase apiKey 的任何想法?

From the firebase docs来自firebase 文档

Unlike how API keys are typically used, API keys for Firebase services are not used to control access to backend resources;与通常使用 API 密钥的方式不同,Firebase 服务的 API 密钥不用于控制对后端资源的访问; that can only be done with Firebase Security Rules (to control which users can access resources) and App Check (to control which apps can access resources).这只能通过 Firebase 安全规则(控制哪些用户可以访问资源)和 App Check(控制哪些应用可以访问资源)来完成。

Usually, you need to fastidiously guard API keys (for example, by using a vault service or setting the keys as environment variables);通常,您需要严格保护 API 密钥(例如,通过使用保管库服务或将密钥设置为环境变量); however, API keys for Firebase services are ok to include in code or checked-in config files.但是, Firebase 服务的 API 密钥可以包含在代码或签入的配置文件中。

Your .env file should have variables like this VITE_SECURE_API_KEY=API-KEY and you can use them in your client-side app using import.meta.env.VITE_SECURE_API_KEY .你的.env文件应该有像VITE_SECURE_API_KEY=API-KEY这样的变量,你可以使用import.meta.env.VITE_SECURE_API_KEY在你的客户端应用程序中使用它们。

dotenv works on the server-side for sveltekit using node-adapter, also the firebase-admin package works only on the node environment. dotenv使用 node-adapter 在服务器端为 sveltekit 工作,firebase-admin 包也仅在节点环境中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM