Google cloud firestore 用户只能访问Firestore中的文档

Google cloud firestore 用户只能访问Firestore中的文档,google-cloud-firestore,firebase-security,Google Cloud Firestore,Firebase Security,我正在努力制定firestore规则 根据屏幕截图,我为用户收集了动态文档ID 我正在尝试为用户设置仅访问其文档的规则 FbId是facebook id(因为它是我应用程序中的身份验证方式) userId是firebase id(不确定保存它是否重要) 以下是我目前的规则: 此规则允许经过身份验证的用户访问整个集合 我如何设置此规则?如果有什么我需要改变的结构 更新:我不想更改用户集合的documentid以匹配userid,因为我有另一个集合,其中用户可以有多个文档;所以这个解决方案并不适合

我正在努力制定firestore规则

根据屏幕截图,我为用户收集了动态文档ID

我正在尝试为用户设置仅访问其文档的规则

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

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

以下是我目前的规则:

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

我如何设置此规则?如果有什么我需要改变的结构

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


谢谢

使用用户的Firebase身份验证UID作为文档的ID会容易得多。Facebook UID在安全规则中不可用

如果使用Firebase UID,则可以编写类似于中的规则以实现基于用户的安全性。点击并阅读“另一个常见模式是确保用户只能读写自己的数据”。这可能就是你想要做的

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}

使用用户的Firebase身份验证UID作为文档的ID会容易得多。Facebook UID在安全规则中不可用

如果使用Firebase UID,则可以编写类似于中的规则以实现基于用户的安全性。点击并阅读“另一个常见模式是确保用户只能读写自己的数据”。这可能就是你想要做的

service cloud.firestore {
  match /databases/{database}/documents {
    // Make sure the uid of the requesting user matches name of the user
    // document. The wildcard expression {userId} makes the userId variable
    // available in rules.
    match /users/{userId} {
      allow read, update, delete: if request.auth.uid == userId;
      allow create: if request.auth.uid != null;
    }
  }
}

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

我现在使用的规则是:

 match /users/{userId} {
    allow update: if request.auth.uid == resource.data.userId;
 }

谢谢

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

我现在使用的规则是:

 match /users/{userId} {
    allow update: if request.auth.uid == resource.data.userId;
 }

谢谢

除了将文档Id更改为与用户Id相同之外,还有其他方法吗?因为在另一个集合中,用户可以匹配多个文档,因此它将无法与另一个集合一起工作。当然,您也可以使用规则中的Firebase UID检查字段的内容。文档也可以在这方面为您提供指导。我正在尝试这样做:match/users/{document=**}{allow read,write:if request.auth.uid==request.resource.data.userId}request.resource.data.userId->它不会从用户文档中获取userId另一个更新:match/users/{user\id}{允许读取,更新:if request.auth.uid==resource.data.user_id;}这就是我现在正在做的。user\u id是我数据库中的新字段,它有firebase user id.resource.data.user\u id->这实际上就是问题所在;即使我现在试图对其进行硬编码,它也不会从数据库返回数据。除了将文档id更改为与user id相同之外,还有其他方法吗?因为在另一种情况下集合用户可以匹配多个文档,因此它不会与另一个集合一起工作。当然,您也可以使用规则中的Firebase UID检查字段的内容。文档还可以在这方面为您提供指导。我正在尝试这样做:match/users/{document=**}{allow read,write:if request.auth.uid==request.resource.data.userId}request.resource.data.userId->它没有从用户文档中获取用户id另一个更新:match/users/{user\u id}{allow read,update:if request.auth.uid==resource.data.user\id;}这就是我现在正在做的。user\u id是我数据库中的新字段,它有firebase user id.resource.data.user\u id->这实际上就是问题所在;即使我现在试图对其进行硬编码,它也不会从数据库返回数据。