用户只能访问他/她在 Firestore 中的文档

Aza*_*bAF 1 firebase-security google-cloud-firestore

我正在努力制定 Firestore 规则。

根据屏幕截图,我收集了具有动态文档 ID 的用户。

在此输入图像描述

我正在尝试为用户设置一条规则,使其只能访问他/她的文档。

FbId 是 facebook id(因为它是我的应用程序中的身份验证方式)

userId 是 firebase id(不确定保存它是否重要)

这是我目前的规则:

match /users/{document=**} {
  allow read, write: if normalUser();
  }

  function normalUser() {
  return request.auth.token.firebase.sign_in_provider == "facebook.com"
  && request.auth.uid != null;
}
Run Code Online (Sandbox Code Playgroud)

此规则允许经过身份验证的用户访问整个集合。

我该如何设置这个规则?如果结构中有什么需要改变的吗?

更新:我不想更改用户集合的 documentid 以匹配 userid,因为我有另一个集合,其中用户可以拥有多个文档;所以这个解决方案并不适合所有情况。

谢谢

Aza*_*bAF 6

我从应用程序获取的方法是在查询中使用 facebook id,这就是它不起作用的原因。

我现在使用的规则:

 match /users/{userId} {
    allow update: if request.auth.uid == resource.data.userId;
 }
Run Code Online (Sandbox Code Playgroud)

谢谢