gpt4 book ai didi

firebase - 如何制定云Firestore安全规则,使登录用户只能访问他们的数据?

转载 作者:行者123 更新时间:2023-12-02 02:20:35 24 4
gpt4 key购买 nike

我是 Cloud Firestore 新手,无法完成任务。我希望每个登录用户都应该能够看到自己的数据,即每个登录用户只能看到他/她创建的数据,而看不到其他人的数据。目前,当我使用不同的用户登录时,每个用户都可以看到 Firestore 数据库中存储的所有内容。我尝试了很多教程和文档,但我的情况似乎有所不同,我无法在我的应用程序中应用他们的方法。

请查看下面给出的我的 Firestore 数据库的屏幕截图:

这是“用户”集合,其中文档 ID 与用户使用电子邮件密码登录方法登录时提供的 Firebase uid 相同: This is the 'users' collection

这是“venues”集合,其中文档 ID 是自动生成的,字段值是由用户在应用程序中输入的。 enter image description here

这是我的 Firestore 安全规则中的代码:

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid} {
allow read, write: if request.auth != null && request.auth.uid == uid;
}
match /users/{uid} {
allow read: if request.auth != null;
}
match /{document=**} {
allow read, write: if request.auth != null;
}
}
}

请帮帮我!

最佳答案

仅对一个路径声明一次安全规则 - 您用于 users/{uid} 第一次正确设置,并在它们旁边允许读取所有登录者

rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /users/{uid} {
allow read, write: if request.auth != null && request.auth.uid == uid;
}}}

request.auth.uid == uid 规则通常用于让用户仅访问以其 id 命名的文档。你在第一个声明中就说对了。

resource.data.nameOfTheField == 'fieldContent' 如果 firestore 中的 nameOfTheField 已包含 fieldContent 值,则用户可以访问文件。

request.resource.data.nameOfTheField == fieldValue 规则会验证操作,如果 nameOfTheField 的请求数据将更改为 fieldValue

您可以允许基于其他集合数据的操作:get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true 如果用户在用户的管理字段中是管理员,则允许数据操作文档。

请记住,您可以使用解析为 bool 值的任何运算符进行验证,因此您可以在验证整数字段值时使用 > <。

关于firebase - 如何制定云Firestore安全规则,使登录用户只能访问他们的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66478952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com