【问题标题】:Firebase rules checking an existing resource data but returning permission deniedFirebase 规则检查现有资源数据但返回权限被拒绝
【发布时间】:2021-09-15 00:14:36
【问题描述】:

当经过身份验证的用户尝试创建包含两个字段的帖子时,这些 firebase 规则返回权限被拒绝,一个包含用户 id(uid) 的 authorId 字段和一个包含 'published' | 'draft'editStatus 字段

   match /posts/{post} {
        allow read: if request.auth != null && resource.data.editStatus == 'published' 
            || request.auth.uid == resource.data.authorId;
        allow write: if  request.auth != null && request.auth.uid == resource.data.authorId;
      }

有人可以帮我吗?

【问题讨论】:

  • 您能分享一下您使用的 Firebase 版本吗?
  • 请也分享一个示例文档的屏幕截图:D
  • 安全规则本身没有任何作用。请编辑您的问题,以显示任何使用者可以运行的最小、完整/独立代码,以根据这些规则重现问题。

标签: reactjs react-native google-cloud-firestore firebase-security


【解决方案1】:

如果您正在创建新文档,则无法检查 resource.data.authorID,因为此时该资源不存在。相反,您需要查看来自request.resource.data.authorID 的请求值。

我还强烈建议您将您的安全规则分解为细化条件,以尊重地处理创建与更新。

来源:https://firebase.google.com/docs/firestore/security/rules-structure#granular_operations

【讨论】:

  • 非常感谢 Digi Byte -- 是的,就是这样!
猜你喜欢
  • 2019-12-26
  • 1970-01-01
  • 2020-08-16
  • 1970-01-01
  • 1970-01-01
  • 2019-03-10
  • 2021-05-25
  • 2018-02-20
  • 2016-11-27
相关资源
最近更新 更多