Firebase云Firestore数据库规则数据访问被拒绝

Firebase云Firestore数据库规则数据访问被拒绝,firebase,google-cloud-firestore,Firebase,Google Cloud Firestore,我有一个应用程序与cloud firestore通信,根据其uid获取用户数据,并且在当前设置下工作正常: service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write; } } } 然而,这显然是不可接受的,因为它的公众和来自firestore的警告: 您的安全规则定义为public,因此任何人都可以窃取、

我有一个应用程序与cloud firestore通信,根据其uid获取用户数据,并且在当前设置下工作正常:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write;
    }
  }
}
然而,这显然是不可接受的,因为它的公众和来自firestore的警告:

您的安全规则定义为public,因此任何人都可以窃取、修改或删除数据库中的数据

下图是我当前的数据库设置,集合由唯一的用户uid隐藏以保护用户隐私。。。我希望通过Firestore规则来实现的是,只允许uid通过读写访问权限访问自己的集合uid

我尝试了以下规则,但无法访问数据:

service cloud.firestore {
  match /database {
    match /{userId} {
      allow read, write;
    }
  }
}
感谢您为我指点写规则的正确方法!我读了firestore指南,但一点帮助都没有


非常感谢

如果希望用户读取/写入其集合中的文档,请尝试以下操作:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // collection. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /{userId}/{userDocs} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}
如果您有嵌套数据,并且希望用户访问其所有嵌套数据,请尝试:

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // collection. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /{userId}/{userDocs=**} {
      allow read, write: if request.auth.uid == userId;
    }
  }
}
文件:


谢谢!那很有效!我使用了第二个选项,因为我希望用户访问他们的所有嵌套数据。仅供参考:UID不是私人问题。也就是说,尽管我知道你的ID是3022454,但我不能在堆栈溢出时对你的帐户进行任何恶意操作。看见