【问题标题】:Firebase realtime database rules for a chat app聊天应用的 Firebase 实时数据库规则
【发布时间】:2023-04-04 19:46:01
【问题描述】:

试图将我的问题放在其一般概念中,以便您的回复与所有被卡在聊天应用程序的 firebase 规则的人保持相关。

我的聊天数据库很简单,有 3 个节点:Messages / Connections / Users,如下所示:

数据库

-Messages
--message : 
--$uid
...

-Connections
--Connection1
--Connection2
--ConnectionX
...

-Users
--UID1
--UID2

这是我需要为我的实时数据库规则校对 pls 的内容:

{
  "rules": {
    "users": {

      "$uid": {
        ".read": "auth != null" && "$uid/current_page" == "3w_stringofmywebsite/*",
        ".write": "auth != null" && "$uid/current_page" == "3w_stringofmywebsite/*"
      }

      // .read & .write to authenticated users & variable $uid/current_page= "3w_stringofmywebsite/*"  (allow access from requests from all pages from my web site only)
    }

    "connections": {
      "$uid": {
        ".read": "auth != null" && root.child(‘users’).child($uid).child(“current_page ") == "
            3 w_stringofmywebsite/*"
      }
                
      // .read to authenticated users & variable from another path “users/$uid/current_page” = "3w_stringofmywebsite/*"  (allow access from from all pages from my web site only)
    
    }
    
    "messages": {
      ".read" : "auth != null" && root.child(‘users’).child($uid).child(“current_page") == "3w_stringofmywebsite/*" ,
      ".validate": "auth!=null" && data.child('msg').val ().length <= 140 && data.child('msg').isString"
    }
    
    // .read to authenticated users & variable from another path “users/$uid/current_page” = "3w_stringofmywebsite/*"  (allow access from all pages from my web site only)
    // .write to authenticated users & variable “msg” is a string & < 140 characters
    
  }

感谢您对上述代码的慷慨更正,以便它为所有其他新人服务。

【问题讨论】:

  • 安全规则自己不会做任何事情。它们只有在您的应用程序代码使用后才变得有意义。请编辑您的问题,以显示未按预期执行的代码,并清楚地描述问题。

标签: firebase-realtime-database chat firebase-security


【解决方案1】:

谢谢你亲爱的弗兰克,

命令的语法有很多错误(尝试了很多次更正但列表很长),我不想在这里填满初学者的错误。

*对于“用户”节点: 阅读:只有经过身份验证的连接和来自我的网站的连接的权利(因为聊天工具在这个网站上) 写:与上面类似。

对于“连接”节点: 读取经过身份验证的用户和来自我的网站的连接[有一个变量“current_page”来自另一个路径“users/“ABC”/current_page”=“3w_stringofmywebsite/”,我可以使用它来匹配字符串我的网站作为测试

注意:上面的“ABC”是节点自动生成的字符串,我不知道如何指向它下面的变量“current_page”,因为它从一个节点条目到另一个节点条目不断变化!

*对于“消息”节点: 阅读:通过身份验证的权限并再次检查变量“users/“ABC”/current_page”是否来自我的网站 写入:经过身份验证的特权,消息是

感谢您对此的支持。 谢谢。

【讨论】:

    猜你喜欢
    • 2021-01-31
    • 1970-01-01
    • 2017-11-16
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多