google-cloud-firestore - 用户只能在 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; }
此规则授予对整个集合的经过身份验证的用户的访问权限。
如何设置此规则?如果有什么我需要改变结构?
更新:我不想更改用户集合的文档 ID 以匹配用户 ID,因为我有另一个集合,用户可以有多个文档;所以这个解决方案并不适合一切。
谢谢
解决方案
使用用户的 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;
}
}
}
推荐阅读
- php - 在 php、mysql 中以“内联编辑”样式编辑回显输出-
- jquery - HTML 访问密钥 (alt+s) 在 CK 编辑器中不起作用
- powershell - 如何使用 powershell 中的命令将 sudo 用户添加到 WSL?
- fusionauth - 是否可以在用户注册期间强制执行 2FA 配置?
- ruby - Rails:Active Storage 变体生成一个附加文件
- angular - JSON值降序排序的问题
- sql - 如何编写只选择一些字母的sql?
- php - 如何在 php-fpm 后面的 docker 中获取 laravel 的日志?
- c# - 同步 dbcontext 查询列表重写为单独上下文上的异步调用
- node.js - 提交谷歌助手时打开麦克风问题