首页 > 解决方案 > 在谷歌云中的安全规则之后,cluase 在哪里不起作用

问题描述

这是我的 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;
    }
  }
}

为什么 ?

标签: firebasegoogle-cloud-firestorefirebase-security

解决方案


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


推荐阅读