gpt4 book ai didi

node.js - Google App Engine Node.js 应用中 Firebase 项目的 serviceAccountKey.json 的安全路径

转载 作者:太空宇宙 更新时间:2023-11-03 22:51:23 26 4
gpt4 key购买 nike

我有一个 Firebase 项目,用户可以与该项目共享一些由媒体和文本组成的内容。为了让 Facebook 正确处理链接,共享内容必须有一个预先填充的页面(在服务器端动态创建,而不是使用 Firebase JS API 在客户端创建)。

因此,我决定使用 Google App Engine (GAE) 将这些内容作为 URL 提供,例如 somedomain.com/?content=hfe84gfjy45xs

var admin = require("firebase-admin");

// Fetch the service account key JSON file contents
var serviceAccount = require("path/to/serviceAccountKey.json");

// Initialize the app with a null auth variable, limiting the server's access
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://databaseName.firebaseio.com",
databaseAuthVariableOverride: null
});

path/to/serviceAccountKey.json 文件放入项目文件夹中是否安全?我是否必须将文件放入 Google Cloud Storage 实例?

提前致谢。

最佳答案

express-firebase有一个很好的例子来实现这一点。

  1. serviceAccountKey.json 添加到项目中,但在 .gitignore 中引用该文件路径,这样就不会将其 checkin 源代码管理。

  2. 创建一个 .env 变量,它是 serviceAccountKey.json 的路径,如下所示:

    FIREBASE_SERVICE_ACCOUNT_KEY_PATH=./service-account-key.json

    参见dotenv用于在 JS 应用中加载环境变量。

  3. 从那里,您可以像这样简单地引用您的服务帐户文件并将其传递给 cert 函数:

    const serviceAccount = require(process.env.FIREBASE_SERVICE_ACCOUNT_KEY_PATH);

    admin.credential.cert(serviceAccount)

关于node.js - Google App Engine Node.js 应用中 Firebase 项目的 serviceAccountKey.json 的安全路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42453535/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com