谷歌云中的安全规则后,聚类不起作用

时间:2019-10-01 06:23:50

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

这是我针对firebase的安全规则,它会向查询抛出权限:

app.firestore().collection('users').where('email', '==' ,'test@gmail.com').get().then(snap => console.log(snap.docs[0].data())).catch(err => console.log("err",err));

以上查询引发权限错误!

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}

为什么?

2 个答案:

答案 0 :(得分:1)

您的安全规则是说,用户只能以其验证的UID相同的ID读取用户中的文档。但是您的查询正在尝试读取电子邮件字段具有一定价值的任何文档。由于该查询可能会尝试读取与UID匹配的文档以外的文档,因此该查询每次都会失败。根据您现在拥有的规则,只允许客户端get()他们自己的特定文档,不允许其他查询。

答案 1 :(得分:0)

您必须记住,安全规则不是过滤器

示例:

我的uid:123123

doc1 :    电子邮件:test@gmail.com,ID:123123

安全规则将通过doc1。

doc2 :电子邮件:test@gmail.com,ID:321321

doc2的安全规则将失败。

那是因为您的安全规则是说,如果 ALL 文档中包含uid,则该ID应该可以通过,但是由于没有,它会失败。

要解决此问题,您需要提供有关如何设置数据库以及查询什么的更多信息。也就是说,您正在找到符合条件或其他条件的所有电子邮件。

相关问题