首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的Firebase规则

简单的Firebase规则
EN

Stack Overflow用户
提问于 2016-02-16 02:11:52
回答 1查看 169关注 0票数 0

这是我在firebase上的模型:

代码语言:javascript
复制
Texts
    -KAa-aU_RjZwM7FLQcWt
         id: "5f9fe424-4323-4370-a280-9cb216dc6410"
         text: "gregegreg"
    -KAa-bZC2ouIRQ54YWWr
          id: "5f9fe424-4323-4370-a280-9cb216dc6410"
          text: "gregegreg"

以下是我的规则:

代码语言:javascript
复制
"rules": {
  "Texts": {
      ".read": "auth.id === data.child('id').val()",
      ".write": true
   }

}

不幸的是,当我使用另一个UID而不是5f9fe424-4323-4370-a280-9cb216dc6410登录时,我仍然能够阅读我所有的模型文本

任何想法都很感谢谢谢!

身份验证部分与angularJs和login-password配合使用:

代码语言:javascript
复制
var firebaseObj = new Firebase("https://blinding-heat-xxxx.firebaseio.com");
            $scope.authObj = $firebaseAuth(firebaseObj);

                $scope.login = function() {

                  $scope.authData = null;
                  $scope.error = null;

                  $scope.authObj.$authWithPassword({
                        email: $scope.email,
                        password: $scope.password
                    }).then(function(authData) {
                      console.log("Logged with id:", authData.uid);

                      $scope.loadData();

                    }).catch(function(error) {
                      console.error("Auth Failed :", error);
                      $scope.error = error;
                    });
                };

最终将数据加载到网页上,如下所示:

代码语言:javascript
复制
$scope.loadData = function(){
var ref = new Firebase("https://blinding-heat-xxxx.firebaseio.com"); 
$scope.Texts = $firebaseArray(ref.child('Texts'));

}

在HTML中,angularJs就在那里:

代码语言:javascript
复制
 <div ng-repeat="text in Texts">text.text</div>

真正奇怪的是,下面的规则运行得非常好,例如:

代码语言:javascript
复制
"rules": {
  "Texts": {
      ".read": " auth != null ",
      ".write": true
   }

}

EN

回答 1

Stack Overflow用户

发布于 2016-02-16 02:21:07

就快到了!它实际上是auth.uid,而不是auth.id

此外,您还需要提供一个通配符,即一个$变量,用于将规则应用于/Texts下的任何子级。

代码语言:javascript
复制
"rules": {
    "Texts": {
      "$text_id": {
        ".read": "auth.uid === data.child('id').val()",
        ".write": true
      }
    }
}

如果不使用通配符,该规则将应用于/Texts的静态路径。

这是模拟器中的输出。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35416245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档