Firestore安全规则缺少权限Angular

时间:2018-01-28 11:19:28

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

我想设置Cloud Firestore规则以仅返回由登录用户创建的帖子。创建帖子时,我将帖子文档中的用户ID保存为“author_id”。

我能够写入数据库,但无法阅读以下安全规则:

service cloud.firestore {
  match /databases/{database}/documents {
    match /posts/{post}{
      allow write: if request.auth.uid != null;
      allow read: if request.auth.uid == resource.data.author_id
    }
    match /friends/{friend=**}{
        allow read, write: if request.auth != null;
    }
  }
}

我做错了什么?

我手动比较了两个id,它们是相同的。 当我创建虚拟条件(我知道应该解决为真)时,例如:

allow read: if resource.data.name is string

即便如此,我也会遇到同样的错误。

我得到的错误如下:

Error: Missing or insufficient permissions.
    at new FirestoreError (error.js:149)
    at JsonProtoSerializer.fromRpcStatus (serializer.js:93)
    at JsonProtoSerializer.fromWatchChange (serializer.js:573)
    at PersistentListenStream.onMessage (persistent_stream.js:391)
    at eval (persistent_stream.js:333)
    at eval (persistent_stream.js:308)
    at eval (async_queue.js:83)
    at ZoneDelegate.invoke (zone.js:388)
    at Object.onInvoke (core.js:4733)
    at ZoneDelegate.invoke (zone.js:387)

谢谢。

更新:

我意识到规则不是过滤查询并返回修改后的快照。我在前端修改了我的查询以过滤结果,现在按设计工作。

1 个答案:

答案 0 :(得分:0)

您用来检索帖子的查询是什么?

查询的结果集必须符合规则。您的查询应包含与规则匹配的where子句:

var user = firebase.auth().currentUser;

ref.where("author_id", "==", user.uid)