如果Firebase Firestore文档的属性与request属性匹配,请阅读该文档

时间:2020-08-27 02:12:42

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

我确定这是一个受欢迎的问题,但我找不到该问题/答案!

  • 目标:如果当前登录用户的uid与文档的uid匹配,请阅读Firestore。
  • 当前结果:尝试阅读时出现错误。 Uncaught Error in snapshot listener: FirebaseError: Missing or insufficient permissions.

这是我的Firestore规则:

service cloud.firestore {
  match /databases/{database}/documents {
  
    match /cars/{document} {
        allow read: if request.auth.uid == document.uid;
    }
   
  }
}

以下是示例数据: enter image description here

1 个答案:

答案 0 :(得分:2)

如果要在规则中引用文档数据,语法如下:

allow read: if request.auth.uid == resource.data.uid;

当客户端执行单个文档的get(),并且使用与文档的uid字段匹配的UID登录时,这将起作用。

我建议查看authenticationdata validation上的文档。