允许读取访问 Firebase Firestore 中的一个特定集合

Apo*_*ocy 1 firebase firebase-security firebase-authentication google-cloud-firestore

在我的 android 应用程序中,我使用 Firebase Firestore 存储一组“用户名”,以便在用户登录之前进行查询,以检查所选用户名是否可用。

目前我的项目有 2 个主要集合:“用户”和“用户名”。我试图仅在“用户名”集合中允许读取访问。我的程序失败,因为当我尝试在用户通过身份验证之前查询“用户名”集合时,它说我没有正确的权限。

我的规则非常基本,但我想知道是否可以以某种方式修改它们以使我的项目正常工作。规则如下:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

firestore 数据库的视觉概念:

在此处输入图片说明

Dou*_*son 12

如果您想将“用户名”集合与其他所有内容区别对待,请按名称调用它,并为每个人分配完全读取权限:

    match /usernames/{id} {
      allow read: if true;
    }
Run Code Online (Sandbox Code Playgroud)

我建议花一些时间研究有关安全规则文档,以更好地了解它们的工作原理,并学习如何自己制定规则。

  • 在 Stack Overflow 上,习惯上单击复选框将答案标记为正确。 (2认同)