【问题标题】:Allow user to read only own content with firebase rules?允许用户使用 Firebase 规则读取自己的内容吗?
【发布时间】:2016-12-23 04:55:00
【问题描述】:

我正在尝试将用户设置为仅获取自己的对象。我有一个在 fb 数据库上有 3 个属性的任务:任务文本、日期时间、用户。用户有用户 uid。我想写一条规则,让用户只能读取自己的任务。

到目前为止,我有这个,但它不起作用:

{
    "rules": {
        "tasks": {
            ".read": "auth.uid == data.child('user').val()",
            ".write": "auth.uid != null",
        }
    }
}

【问题讨论】:

  • 如果你将它重组为:root > userId > task(s),那么你可以这样做:rules > $uid > ".read" : "auth != null && $uid= =auth.uid”。
  • 谢谢林西!效果很好。

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


【解决方案1】:

您需要按用户分解任务,因此您的结构如下所示:

root
  tasks
    Puf
      ...
    Andrew
      ...
    James
      ...

这可以通过如下规则来保护:

{
    "rules": {
        "tasks": {
            "$userId": {
                ".read": "auth.uid == $userId",
                ".write": "auth.uid == $userId",
            }
        }
    }
}

【讨论】:

  • 感谢您的回复。实际上,Linxy 的评论对我有用。
猜你喜欢
  • 2015-03-25
  • 2019-09-24
  • 2019-01-10
  • 2021-02-07
  • 2020-07-11
  • 1970-01-01
  • 1970-01-01
  • 2018-12-08
  • 1970-01-01
相关资源
最近更新 更多