【问题标题】:Creating/Adding user on Firebase在 Firebase 上创建/添加用户
【发布时间】:2015-12-05 23:30:37
【问题描述】:

我一直在玩 Firebase,但遇到了一个问题,我试图查看一些文档,但找不到正确的答案。

问题是 Firebase 提供了用户身份验证,这很好,但我可以对这个用户设置一些限制吗?所以它只能访问我的数据库方案的一部分吗?例如:我的数据库上有两个组:

https://MYFIREBASE.firebaseio.com/group1/posts/ https://MYFIREBASE.firebaseio.com/group2/posts/

哪一个有不同的用户,不同的帖子。

谢谢!!

【问题讨论】:

    标签: javascript firebase firebase-security


    【解决方案1】:

    You're looking for Security Rules.

    安全规则是限制对 Firebase 数据库的访问的服务器端验证。

    以下是只读数据库的规则。

    {
      "rules": {
         ".read": true,
         ".false": false
      }
    }
    

    安全规则提供了保存重要服务器端信息的服务器变量。 The variable you'll be interested in is the auth variable.

    auth 变量允许您检查尝试访问数据库的用户是否经过身份验证。

    {
      "rules": {
        "group1": {
           ".read": "auth !== null",
           ".write": "auth !== null"
        }
      }
    }
    

    根据上述规则,只有经过身份验证的用户才能访问/group1 位置。

    但对于基于用户的安全性,您需要使用用户的uid 进行索引。

    {
      "rules": {
        "group1": {
           "posts": {
             "$uid": {
               ".read": "auth.uid == $uid",
               ".write": "auth.uid == $uid"
             }
           }
        }
      }
    }
    

    using wildcards (which are basically route parameters),您可以检查尝试访问数据的用户是否拥有该数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-05
      • 2017-12-26
      • 2020-11-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多