angular - Angular 和 Firebase:如何设置 Cloud Firestore 安全规则?
问题描述
让我问你如何在 Cloud Firebase 控制台中设置安全规则。我研究了官方 Firebase 文档https://firebase.google.com/docs/firestore/security/rules-conditions?authuser=0和这篇文章https://angularfirebase.com/lessons/firestore-security-rules-guide /,但我无法弄清楚我的应用程序中安全规则的行为。
有什么问题?
首先,我想从非常基本的规则类型开始。如果用户未经身份验证(未登录),我想允许用户访问产品集合并阻止访问订单集合。
我的规则如下所示:
service cloud.firestore {
match /databases/{database}/documents {
match /categories/{category} {
allow read, write;
}
match /products/{product} {
allow read, write, list, get, update;
}
match /orders/{order} {
allow read, write: if request.auth.uid != null;
}
}
}
以及此时它是如何工作的:
我只能在我的应用程序的管理面板中阅读、编写产品分类(这样我就可以管理产品),但在客户方面,产品被阻止了。在 Firebase 控制台中,此规则通过了测试(请参阅截图),但在开发控制台中,我可以看到一条消息:“权限缺失或权限不足”。10分钟过去了,结果还是一样。我还不清楚,因为我已经设置了一个允许随时访问产品的规则(第 9 行)。
第二个问题是,如果我测试订单排序,我有一个错误“错误:simulator.rules 行 [13],列 [29]。空值错误(见截图)。
其他有用信息:
- 角6 | Firebase 5.2.0 | AngularCLI 6.0.8 | 通过 Google 的身份验证方法
谢谢你!
解决方案
在模拟器中,您尚未打开“已验证”开关。一旦你打开它,传递的请求将具有 auth 值。目前,它为 null,因此您在尝试访问 Orders 集合时遇到错误。
推荐阅读
- c++ - 通过附加和克隆子字符串来构建字符串的算法
- python - 如何更改 sqlalchemy 的 create_engine 中的编码选项?
- python - 用户提供的文件中的 2 个 ascii 字符计数器
- angular - 如何在另一个 Angular 组件中显示 Angular 组件
- string - 如何使用 sparql 在字符串中查找特定字符的位置
- c++ - 如何正确实现 C++ 类析构函数
- pdf-generation - Hybris MediaModel 如何获取字节
- c# - DICOM多平面图像重建
- python - 如何为 PyTorch Estimator 指定 python==3.6.8(conda_packages 不够)
- wordpress - 在帖子网格中悬停时显示不同的图像