Firebase规则:未知变量'$uid' [英] Firebase rules: Unknown variable '$uid'

查看:15
本文介绍了Firebase规则:未知变量'$uid'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户只能访问他们自己的内容,除了一个子节点:common
common子节点中,我希望所有登录用户都可以访问。 我制定了以下规则:

{
  "rules": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      },
        "common" : {
        ".read": "auth != null && auth.uid == $uid",
        ".write": "auth != null && auth.uid == $uid"
      }
    }
  }    

Firebase显示错误:

保存规则时出错-第8行:未知变量"$uid"。

错误出现在此行:".read": "auth != null && auth.uid == $uid",

推荐答案

根据您的问题,以下是您所需的数据库结构:

{
  "userIdA": { // anything here can be written by only userIdA
    "name": "Tom", // this is just example data
    "location": "London",
    /* ... */
  },
  "userIdB": { // anything here can be written by only userIdB
    "name": "Sarah",
    "location": "New York",
    /* ... */
  },
  /* ... other user data ... */
  "common": { // anything here can be written by signed in users
    "data1": "some value",
    "data2": "some other value",
  }
}

此结构的规则为:

{
  "rules": {
    "common" : {
      ".read": "auth != null", // logged in users can read
      ".write": "auth != null" // logged in users can write
    },
    "$uid": { // $uid will be the value of any key, that isn't listed above it (in this case, anything other than "common")
      ".read": "$uid === auth.uid", // only the matching user can read
      ".write": "$uid === auth.uid" // only the matching user can write
    }
  }
}

注意:此数据结构不太安全。仅允许对数据库中需要的内容进行读/写访问。有了这种结构,任何用户都可以打开他们的控制台,删除"/common"下的所有内容。您可以考虑添加".validate"规则以确保某些键(如"/common/data1")只是字符串。

这篇关于Firebase规则:未知变量'$uid'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆