【问题标题】:How to create REST APIs for google firestore collection data using nodejs SDK如何使用 nodejs SDK 为 google firestore 收集数据创建 REST API
【发布时间】:2019-06-13 05:53:31
【问题描述】:

我需要为 google firestore 数据上的 crud 操作创建 REST API。

API1:列出集合文档或读取特定文档

API2:在集合下创建新文档

API3:更新文档

API4:删除文档

多个应用将共享同一个 Firestore 项目。

  • 基础集合名称:MyCompany
  • 子文档:MyApp1
  • 子集合名称:ErrorMessage
  • 子文档:{"error":"Unauthorized"}

Firestore 数据如下所示:

  • MyCompany.MyApp1.ErrorMessage
  • MyCompany.MyApp2.ReferalCodes
  • MyCompany.MyApp3.AnotherCollection

所有应用都应该共享同一个 firestore 实例,但只能访问它们各自的集合。

我打算在 header("appidentifier": "MyApp1") 中添加一个请求参数,以识别请求来自哪个应用程序。

要求(通过 API 调用):

  1. 一个应用不应访问其他应用的集合/数据
  2. 需要限制权限,比如app1只能读取数据,app2可以读写。

查询/问题:

  1. 哪个节点模块适合这个(firebase-admin 或 firebase 或 @google-cloud/firestore)
  2. 我们是否需要创建和管理多个 Firestore 客户端实例? (管理员或客户端实例)?
  3. 给出一些我们如何管理规则和 Firestore 客户端实例的设计..

【问题讨论】:

  • 您尝试自己编写什么代码?
  • 我尝试使用使用 service-account.json 文件的 firestore-admin npm 模块创建 API。无法实现其他集合的安全性。阅读:Admin SDK 中的 Firestore API 目前不强制执行规则。!!我需要使用客户端模块(“firebase”)吗?

标签: node.js rest google-cloud-firestore firebase-admin firebase-security


【解决方案1】:

在这里发布解决方案:

  1. 使用 firebase-admin npm 创建 firestore 管理实例

    让 adminInst = return await admin.initializeApp({ 凭证:admin.credential.cert() }, 'admininstance1');

  2. 使用 'uuid' 生成自定义令牌:App1

    等待 adminInst.auth().createCustomToken('App1') .then(函数(customToken){ console.log(customToken); }).catch(异步 (错误) => { });

  3. 使用“firebase”npm 为 App1 创建客户端实例并使用自定义令牌进行身份验证(步骤 2)

    让 clientInst = await firebase.initializeApp(, 'App1');

  4. 使用“uuid”生成自定义令牌:App2

  5. 使用“firebase”npm 为 App2 创建客户端实例并使用自定义令牌进行身份验证(第 4 步)

【讨论】:

    猜你喜欢
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 2019-03-17
    • 2019-08-03
    • 1970-01-01
    • 2021-10-09
    相关资源
    最近更新 更多