firebase - Firebase 规则 - 用户只能编辑其数据
问题描述
我有一个 firebase 项目,我正在使用 FirebaseAuth 和 firebase 数据库。
我只有一个名为 baby 的集合,具有 name (String)、userId (String) 和 votes (Integer) 属性。userId 包含创建者的 user.uid。
我想创建一个规则,以便只有创建者用户才能更新或删除文档。任何人都可以阅读,注册用户可以创建。
所以我有以下规则:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /baby/{userId} {
allow read;
allow create: if request.auth.uid != null;
allow update, delete: if request.auth.uid == userId;
}
}
}
有了这个,任何人都可以阅读,注册用户可以创建。但是,uid=userId 的用户无法更新。有谁知道问题出在哪里?
我检查了数据库,文档的 userID 属性是正确的(与用户 uid 相同)。
谢谢
解决方案
很可能{userId}
不是文档 ID,因此request.auth.uid
与{userId}
确保在创建文档时指定文档 ID,它应该按预期工作。
推荐阅读
- scala - 如何从scala中的for循环返回产生的值?
- sql - 查找员工最多的国家/地区来自 SQL
- c++ - 递归模板类和专门的基本情况导致 MSVC 错误 C1001(在 GCC 和 Clang 上工作正常)
- extjs - 无限网格不再适用于 Ext JS 7.3.1 现代
- docker - 为什么构建 Docker 容器镜像的第一步需要一个基础镜像作为源,在我看来它本身就是一个像 ALPINE 这样的操作系统?
- javascript - 通过 YouTube API 更新默认广播的流标题
- kotlin - Kotlin 混合类型
- python - 如何在 ubuntu 中的 virtualbox 安装驱动器共享文件夹上创建 python3 virtualenv
- python - 我有一个 [Errno 2] 没有这样的文件或目录
- uwp - 如何在 Hololens 2 DirectX12 应用程序中支持屏幕触摸键盘