首页 > 解决方案 > 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;
    }

  }
}

以及此时它是如何工作的:

产品采集测试结果 订单收集测试结果 那么,您介意帮助解决上述问题吗?

其他有用信息:

谢谢你!

标签: angularfirebasegoogle-cloud-firestorefirebase-security

解决方案


在模拟器中,您尚未打开“已验证”开关。一旦你打开它,传递的请求将具有 auth 值。目前,它为 null,因此您在尝试访问 Orders 集合时遇到错误。


推荐阅读