【问题标题】:Firestore Security Rules Permission IssueFirestore 安全规则权限问题
【发布时间】:2019-08-20 12:06:27
【问题描述】:

我在 Firestore 集合中有名为“库存”的文档,其结构如下:

{
    creator: string,
    images: string[],
}

创建者字段是创建文档的用户的 uid。

在我的 Firestore 规则部分,我有以下规则:

service cloud.firestore {
   match /databases/{database}/documents {  
     match /inventories/{inventoryid} {
       allow read, write: if request.auth.id == resource.data.creator;
     }
   }
 }

在我的离子应用程序中,我执行以下操作:

this.inventoryCollection = database.collection<Inventory[]>
    ('inventories', ref => ref.where('creator', '==', auth.currentUserId));

执行时出现以下错误:

Missing or insufficient permissions.

谁能看到我做错了什么?是我的规则有问题吗?还是我的代码调用 firestore 有问题?

【问题讨论】:

  • 你检查过当前用户ID是否正确吗?也许用硬编码值进行测试?
  • @AndréKool currentUserId 是从 Firebase Auth 库中提取的,因此那里没有硬编码值。我已经在下面标记了答案。谢谢。

标签: angular firebase google-cloud-firestore ionic4 firebase-security


【解决方案1】:

您应该使用request.auth.uid 而不是request.auth.id,请参阅https://firebase.google.com/docs/reference/rules/rules.firestore.Request#auth

此外,您应该将您的规则分为两个“子规则”,如下所示:

service cloud.firestore {
   match /databases/{database}/documents {  
     match /inventories/{inventoryid} {
       allow read: if request.auth.id == resource.data.creator;
       allow write: if request.auth.id == request.resource.data.creator;
     }
   }
 }

注意request.resource 用于write 规则。

这在下面关于 Firestore 安全规则的官方视频中得到了很好的解释:https://www.youtube.com/watch?v=eW5MdE3ZcAw

【讨论】:

  • 我讨厌自己。最初我没有使用 where 子句进行过滤,所以我得到了权限错误。因此,在我试图解决这个问题时,我一定是输入了错误的 uid 字段。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
  • 2018-07-07
  • 2019-08-14
  • 1970-01-01
  • 2021-06-13
  • 2021-01-16
  • 2021-02-13
相关资源
最近更新 更多