Firebase 安全规则:如果用户 uid 作为文档存在于集合中则允许

low*_*int 2 firebase firebase-security google-cloud-firestore

我正在收集我的管理员用户 uid 作为“admins”集合中的文档。我想允许用户读取他们的 uid 是否在该集合中作为文档。

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /admins/{document=**} {
      allow write; // Everyone should be able to write
      allow read: if request.auth.uid == ; // Only read if /admins/{youruid}
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

Dha*_*raj 5

您可以用来exists()检查文档是否存在。尝试重构您的规则,如下所示:

match /admins/{document=**} {
  allow read: if exists(/databases/$(database)/documents/admins/$(request.auth.uid)); 
  // Only read if /admins/{youruid}
}
Run Code Online (Sandbox Code Playgroud)

exists()您可以在文档中阅读更多相关内容。