Firestore Firebase数据库规则

时间:2019-10-29 03:25:52

标签: firebase google-cloud-firestore firebase-security

我正在尝试为我的Firestore Firebase数据库设置访问规则。
(阿拉。https://firebase.google.com/docs/firestore/security/get-started

我想要3条规则:

  1. 外,无法访问所有表
  2. public_data对所有人都有read访问权限,并且
  3. user_data仅具有该经过身份验证的用户的read, write
    (而且我猜任何人都应该也可以创建一个新用户..那就是一个奖励规则)

从文档中来看,规则格式似乎很简单,但是使用 console.firebase.google.com>数据库> Cloud Firestore>(我的数据库)>规则>模拟器下提供的模拟器可以得到结果不是我所期望的。

1 个答案:

答案 0 :(得分:0)

这是我最终使用的内容:

https://console.firebase.google.com/u/0/project/[MY_PROJECT_NAME]/database/firestore/rules

rules_version = '2';
service cloud.firestore {

  match /databases/{database}/documents {

    // 2. Table public_data has read access for everyone
    match /public_data/{document=**} {
      allow read, write; 
    }

    // 3. Table user_data has read, write for just that authenticated user
    match /user_data/{userId} {
      allow read, update: if request.auth.uid == userId;
    }
    // (and I guess anyone should be able to create a new user too .. thats a bonus rule)
    match /user_data/{document=**} {
      allow create; 
      // and no one can delete a user
    }
  }

}

请注意,“ 1。无法访问所有表”会自动发生。

请注意,A read rule can be broken into get and list, while a write rule can be broken into create, update, and delete-https://firebase.google.com/docs/firestore/security/rules-structure

我无法确定Firestore>规则提供的模拟器,也无法确定要放入哪个模拟器。
但是,对数据进行查询是如此容易,以至于我自己进行了测试。

我正在使用Flutter,所以this package,并且在Firebase文档中有您喜欢的语言的示例,例如。 here is a read