node.js - 为每个登录用户初始化管理 Firebase 应用程序

标签 node.js firebase firebase-admin

我正在 NodeJS (expressjs) 中创建一个网站,该网站将使用 Firebase Admin Auth API 使用 id-token-verification 对用户进行身份验证.

此 token 随后将保存到服务器 session 中,用于从服务器对实时数据库进行查询。

我的问题是关于限制权限,文档说 databaseAuthVariableOverride 应该用于模仿使用其 uid 的用户

Taken from the admin auth docs:

If you want your server to emulate user actions like accessing the Firebase Realtime Database as that user, you should first verify and decode an ID token for that user. Then, make use of the databaseAuthVariableOverride option to limit the privileges of your server

因此对于每个用户:

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://databaseName.firebaseio.com",
  databaseAuthVariableOverride: {
    uid: userId1
  }
}, userId1)

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://databaseName.firebaseio.com",
  databaseAuthVariableOverride: {
    uid: userId2
  }
}, userId2)

为每个登录用户初始化一个新的 firebaseApp 是否可行,或者是否有标准方法来处理多个用户的权限限制?

最佳答案

这并不完全是管理 SDK 的用途。您当然可以通过这种方式模拟用户,但这不是最佳实践。

用户应该通过客户端读写自己的数据。他们存在安全规则,因为您不能相信他们会按照您想要的规则行事。

在服务器上,由于您控制所有逻辑,因此不需要模拟最终用户,因为您可以相信您自己的代码将对其读取和写入的任何数据执行正确的操作。

uid 属性旨在实现对您控制的另一种类型的管理角色的临时权限下降,而不是模拟您的最终用户。该 uid 通常会被硬编码在定义该角色或服务的权限级别的安全规则中。关于其工作原理的更好描述 can be found here 。 (这是您引用的文档中“此处”一词的链接。)

关于node.js - 为每个登录用户初始化管理 Firebase 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42989845/

相关文章:

firebase - 如何按 '_createTime' 对 Firestore 文档进行排序?

reactjs - 如何将 firebase 的 firestore/admin sdk 与 next.js 一起使用

node.js - Electron 和 webpack 使用任务构建

node.js - 为 Nodejs 递归扫描 AWS Dynamo DB 的函数

node.js - 如何修复 ('throw er;//Unhandled ' 错误“事件”)代码生命周期?

java - 如何验证 Firebase ID token

与大写字母相关的Javascript正则表达式无限循环

android - 使用多个 Firebase 项目终止应用程序时未收到 FCM 通知

android - 从 Cordova 访问 Google 凭据

javascript - 如何访问 Firestore 中查询文档的子集合?