如何使用 Firebase 数据库规则防止节点被删除?

How to prevent a node from being deleted using Firebase Database rules?

我有一个 firebase 数据库,只有 3 个主节点。这是一个聊天应用程序,下面是结构

chat_messages
    - chatID
        -messageID
            -message (string)
            -read (bool)
            -senderName (string)
            -senderProfilePic (string)
            -sent_by (string)(uid)
            -timestamp (string)
 
chat_room
    -chatID
        -chat_mode (string)
        -last_message (string)
        -timestamp (string)
        -members
            -uid (string)
                -email (string)
                -name (string)
                -photo (string)
                -uid (string)
                -userID (int)
                -userRole (string)

user_chatrooms
    -uid
        -chatID( string) : timestamp (string)

我设置了基本安全性,允许 readwritetrue,我收到一封来自 firebase 的电子邮件,说我的数据库有 insecure rules

然后我重新配置了数据库规则如下

{
  "rules": {
    "chat_messages":
    {
      ".read": "auth != null",
        ".write": "auth != null",
    },
    "chat_room":
    {
      ".read": "auth != null",
        ".write": "auth != null",
    },
    "user_chatrooms":
    {
      ".read": "auth != null",
        ".write": "auth != null",
    }
    
  }
}

这通过阻止访问上述节点之外的任何其他内容来提供额外的保护。

现在如何防止这些节点被删除?

为了防止节点被删除,您可以检查写入操作中是否提供了新数据:

".write": "auth !== null && newData.exist()"