在Firebase中检索文档

时间:2020-03-30 06:44:17

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

我在firebase上有一个数据库,我想检索一个我知道其名称的特定文档(称为documentA),并且在服务器上也有一个工作规则。

如何获得一个完整的安全规则文档?我正在尝试使用:val docRef = db.collection("cities").document("SF").get(),但是我不知道如何包括用户身份验证。

数据库具有这样的结构

cars
|
|-documentA
|-documentB
|-documentC

安全规则:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /cars/{carId}  {
      allow read, write: if request.auth.uid == resource.data.userId;
      allow create: if request.auth.uid != null;
    }
  }
}

1 个答案:

答案 0 :(得分:1)

已登录的用户会自动包括Firebase用户身份验证。除了编写代码以登录用户之外,您无法编写任何代码来手动添加身份验证。如果您的安全规则要求经过身份验证的用户,则必须在进行查询之前安排该登录。