用于匿名身份验证的Firebase规则

时间:2016-02-25 15:16:25

标签: authentication ionic-framework firebase angularfire anonymous

我正在对我的应用程序使用匿名身份验证,当用户打开应用程序时,他/她将进行身份验证,并且会话将在应用程序关闭后结束。

我无法弄清楚如何编写读写规则。我希望用户能够在不看到任何其他用户的情况下读取和编写自己的条目。

我已设法对用户进行身份验证,如下面的控制台日志所示。

Firebase结构:

"rounds" : {
    "-KBNYNvCAb-Wywq6yowW" : {
      "Arrow1" : "1",
      "Arrow2" : "1",
      "Arrow3" : "2",
      "Arrow4" : "2",
      "Arrow5" : "1",
      "Arrow6" : "1",
      "Bow" : "Hoyt",
      "Date" : 1456405827389,
      "Distance" : "10m",
      "RoundName" : "Dcedc",
      "Season" : "Indoor",
      "TotalScore" : 8,
      "UID" : "9ddc8eb2-6f1a-4c26-bac3-b5b473104bb6"

读写中的当前代码是:

    {
  "rules": {
    "rounds": {
      "$rounds":{
        ".read": "auth.uid === data.child('UID').val()",
          ".write": "auth.uid === data.child('UID').val()"
        }
      }
    }
}

当我在网址中添加随机生成的表名时,这仅适用于模拟器,例如

rounds/-KBNYNvCAb-Wywq6yowW

获取uid的控制器在我的控制器中:

      app.controller('scoreCtrl', function($scope, roundInfo, equipmentStore){
      ref.onAuth(function(authData) {
            if (authData) {
      $scope.rounds = roundInfo;
      $scope.bow = roundInfo;
      $scope.round = roundInfo;
      $scope.date = new Date();
      $scope.addItem = function() {
        $scope.round.$add({
            "RoundName": roundInfo.name,
            "Distance": roundInfo.distance,
            "Season": roundInfo.season,
            "Date": this.date? this.date.getTime() : null,
            "Bow": $scope.bow.title.title,
            "Arrow1": roundInfo.arrow1,
            "Arrow2": roundInfo.arrow2,
            "Arrow3": roundInfo.arrow3,
            "Arrow4": roundInfo.arrow4,
            "Arrow5": roundInfo.arrow5,
            "Arrow6": roundInfo.arrow6,
            "TotalScore" : +roundInfo.arrow1 + +roundInfo.arrow2 + +roundInfo.arrow3 + +roundInfo.arrow4 + +roundInfo.arrow5 + +roundInfo.arrow6,
            "UID": authData.uid
                    });
                };
            }
      });
  });

圆形工厂:

    app.factory('roundInfo', function($rootScope, $firebaseArray) {
    var roundRef = new Firebase("https://firebaseLink.firebaseio.com/rounds");
        roundInfo = {};
        roundInfo.name = '';
        roundInfo.bow = '';  
        roundInfo.date = '';
        roundInfo.distance = '';
        roundInfo.season = '';
        roundInfo.arrow1 = '';

            return $firebaseArray(roundRef);

});

firebase身份验证控制器:

        var ref = new Firebase("https://firebaseLink.firebaseio.com");
ref.authAnonymously(function(error, authData) {
  if (error) {
    console.log("Login Failed!", error);
  } else {
    console.log("Authenticated successfully with payload:", authData);
  }
  if (authData) {
    console.log("user logged in")
  } else {
    console.log("user logged out:")
  }    
});



    var ref = new Firebase("https://firebaseLink.firebaseio.com");
ref.onAuth(function(authData) {
  if (authData) {
    console.log("Authenticated with uid:", authData.uid);
  } else {
    console.log("Client unauthenticated.")
  }
});

控制台日志确认用户已经过身份验证:

0     450893   log      Client unauthenticated.
1     451712   log      Authenticated with uid:, 9ee68f25-d6ba-4902-9d83-329d0acf996e
2     451713   log      Authenticated successfully with payload:, [object Object

2 个答案:

答案 0 :(得分:1)

您的问题缺少一些重要信息(例如哪些操作失败),但我会在这里采取有根据的猜测。

您最有可能尝试从rounds进行阅读,并且只想找回用户有权访问的孩子。类似的东西:

ref.child('rounds').on('child_added', function(snapshot) {
  ...
})

这不是Firebase安全规则的工作方式。

为了能够阅读或查询某个位置,您必须具有该位置的读取权限。

您已陷入困境的陷阱包含在Firebase文档中的rules are not filters部分。

因此,在您的规则中,由于用户对rounds没有读取权限,因此读取操作将失败。这就是为什么将项目存储在拥有它们的用户下通常更好的原因之一:

user_rounds
  "9ddc8eb2-6f1a-4c26-bac3-b5b473104bb6"
    "-KBNYNvCAb-Wywq6yowW": true
    ...

处理完全相同问题的一些问题:

答案 1 :(得分:0)

尝试将读/写规则添加到父“rounds”

"rules" {
"rounds" {
      ".read":"true",
      ".write":"true", 
   "$ rounds": {....}}}