允许新用户包含和用户特定路径权限的 Firebase 规则

Firebase rule allowing new user inclusion and user-specific path privileges

在阅读了很多教程和 Whosebug 的问题之后,我仍然不知道该怎么做。 所有教程均以以下规则开头:

"users":{
    "$uid":{
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
     }
}

但是如何在新用户登录时允许新用户进入users节点(auth != null)并保持读写用户特定路径权限?

换句话说: 有没有办法让新用户登录后插入users节点,同时限制他只能读写自己的节点?[​​=11=]

另一个问题。我在另一个节点中有一个写规则 newData.exists()。在模拟器中,当我尝试将空数据写入节点时,它会按预期拒绝它,但它允许我将空数据写入同一节点的子节点。节点不应该将规则级联写入其子节点吗?

您上面的规则将匹配 $uidauth.uid。因此,例如,如果用户的 UID 是 cKzMyjImSBX6ybzeCCCjf0qbTym1,他们将只能在数据库中的 /users/cKzMyjImSBX6ybzeCCCjf0qbTym1 处读取和写入,无论该键当前是否存在(如果不存在)写入时存在,它将被创建)。

来自Firebase documentation on rules

Once a user authenticates, the auth variable in your Firebase Database Rules rules will be populated with the user's information. This information includes their unique identifier (uid) as well as linked account data, such as a Facebook id or an email address, and other info.