基于Firebase角色的访问

时间:2019-12-24 09:55:47

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

我想限制访问权限以写一些文档。 因此,我创建了一个“管理员”集合,该集合使用用户的UID作为文档uid,并使用属性“ admin”来检查其值。

我当前正在尝试实现的数据库规则是这样的:

  match /approved-posts/{post} {
        allow read: if true
        allow write: if get(/databases/$(database)/documents/administrators/$(request.auth.uid)).data.admin == true
  }

我的uid是: uYdyhMBAgyZ36ZNF5B2iPb0NdxQ2

所以我在firebase中有以下集合: firebase collection image

但是以某种方式尝试创建新帖子时,出现以下错误:

FirebaseError: Missing or insufficient permissions.
    at new FirestoreError (http://localhost:4200/vendor.js:158627:28)

我在这里做什么错了?

在行尾添加分号(例如,允许读取:如果为true;)没有帮助。

使用模拟器时,出现空值错误:

Null value error

我阅读了https://firebase.google.com/docs/firestore/security/rules-conditions,并尝试了一个更简单的规则。但是我仍然得到一个错误。 check if document exists rule

也许我需要打开任何设置吗?

2 个答案:

答案 0 :(得分:1)

您的规则看起来很好,但是您在读写规则的末尾缺少分号。不确定这是否是解决方案,请尝试添加分号并更新规则。

  match /approved-posts/{post} {
        allow read: if true;
        allow write: if get(/databases/$(database)/documents/administrators/$(request.auth.uid)).data.admin == true;
  }

答案 1 :(得分:0)

哦,没有我的UID为假。 开始时有一个空格。所以我有一个复制粘贴错误。

由于仪表板上的修剪,我没有注意到。只有在我用一个UID重新创建了一个文档而又没有前导空格的情况下,它才能正常工作。

感谢您的帮助:)

相关问题