【问题标题】:Firestore: Missing or insufficient permissions when trying to add a new subcollectionFirestore:尝试添加新子集合时缺少权限或权限不足
【发布时间】:2019-10-16 13:03:45
【问题描述】:

我目前制定了一些基本规则来访问我的 Firestore 数据库。规则详述如下。谁能帮我理解为什么第二个代码块会导致权限错误?我正在尝试将图像添加到尚不存在的子集合中。谢谢

rules_version = '2';
service cloud.firestore {
    match /databases/{database}/documents {
        match /projects/{project} {
            allow read, write: if request.auth.uid != null
        }
        match /users/{userId} {
            allow create
            allow read: if request.auth.uid != null
            allow write: if request.auth.uid == userId
        }
    match /notifications/{notification} {

            allow read: if request.auth.uid != null

        }

    }
}

这是我的应用代码:

await firestore.add(
      {
        collection: "users",
        doc: user.uid,
        subcollections: [{ collection: "photos" }]
      },
      {
        name: imageName,
        url: downloadURL
      }
    );

【问题讨论】:

  • 我不明白这个对firestore.add() 的调用。它看起来不像原生 SDK 提供的任何东西。您是否在 Firestore SDK 之上使用了一些包装器或其他 SDK?
  • 您好,我正在使用 reduxfirestore
  • 您好,我正在使用 reduxfirestore,但如果我完全打开数据库进行读写,它就可以工作。

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


【解决方案1】:

我对“reduxfirestore”一无所知,但看起来您正试图写入用户下的子集合。您的安全规则对子集合中的文档没有任何规定,因此所有这些查询都将被拒绝。如果您希望能够读取和写入文档,则规则必须匹配它们的完整路径,如下所示:

match /users/{uid}/photos/{pid} { ... }

在控制台中读写始终有效,因为它绕过了安全规则。

【讨论】:

    猜你喜欢
    • 2018-04-25
    • 2019-02-26
    • 2022-01-25
    • 1970-01-01
    • 2020-09-01
    • 2023-02-24
    • 2021-12-31
    • 2021-08-24
    • 1970-01-01
    相关资源
    最近更新 更多