如何根据数据设置Firebase实时;实时数据库规则?

问题描述 投票:1回答:1

我想将我的聊天数据设为私有,并且只能由登录用户访问。

我不知道要设置firebase规则。我的数据结构如下所示。

{
  "chat" : {
    "-L1223434cI8qG1eLQUyj" : {
      "User" : {
        "_id" : "66b2-4bac-abe1-fe89a0e29a28",
        "name" : "aaaabbbccc"
      },
      "friendID" : "8bcd-4b62-bb4e-25b7c1df4ca2",
      "text" : "hello",
      "timestamp" : 1573113592492
    }

我想将_id与我在应用程序下拥有的所有经过身份验证的用户进行比较,并希望向仅使用其ID的用户提供访问权限。

database firebase react-native firebase-realtime-database
1个回答
0
投票

您可以使用以下安全规则来获得所需的结果,这些规则利用内置的auth变量来保存有关当前登录用户的信息:

{
  "rules": {
    "chat": {
      "$msg_id": {
        // grants write access to the owner of this message
        // whose uid must exactly match the value of _id
        ".write": "data.child("User/_id").val() === auth.uid",
        // grants read access to the owner or recipient of this
        // message whose uid must exactly match the saved uids
        ".read": "data.child("User/_id").val() === auth.uid || data.child("friendID").val() === auth.uid"
      }
    }
  }
}

[我建议阅读Securing DataUser Based Security文档以获取更多详细信息,这些详细信息比在此重复的方式更好地解释了这些概念。

© www.soinside.com 2019 - 2024. All rights reserved.