0

我正在尝试以下代码。单击“登录”时,我可以通过电子邮件/密码对用户进行身份验证,并返回一个对象;但我不明白我缺少什么来返回我试图从“items”和“alseCards”获取的数据。如果我将读写设置为“true”,它们会返回数据正常,只是想通过任何登录的用户对那些用户进行身份验证。我觉得这是安全/规则设置,但只是开始使用 Firebase 并苦苦挣扎。

索引.html

<head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="https://cdn.firebase.com/js/client/2.2.7/firebase.js"></script>
    <script src="https://cdn.firebase.com/libs/angularfire/1.1.2/angularfire.min.js"></script>
    <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
</head>

<body ng-controller="controller">
    <div>
        <button ng-click="login()">Login</button>

        <p ng-if="authData">Logged in user: <strong>{{ authData.uid }}</strong>
        </p>
        <p ng-if="error">Error: <strong>{{ error }}</strong>
        </p>
    </div>
     <h1>Items</h1>
    <ul id="items">
        <li ng-repeat="item in items">
             <h2>{{item.$id}}</h2>
            <ul id="inspections">
                <li ng-repeat="inspection in item.inspections">{{inspection.description}} on {{inspection.timeStamp}} by {{inspection.inspector}}</li>
            </ul>
        </li>
    </ul>
    <script>

    </script>
    <script>
        var app = angular.module("app", ["firebase"]);
        app.controller("controller", ["$scope", "$firebaseArray", "$firebaseAuth",
                function($scope, $firebaseArrary, $firebaseAuth) {
                    var ref = new Firebase("https://<-testing->.firebaseio.com");
                    var auth = $firebaseAuth(ref);

                    $scope.login = function() {
                        $scope.authData = null;
                        $scope.error = null;

                        auth.$authWithPassword({
                                email: "email@myemail.com",
                                password: "alseTest"
                            }).then(function(authData) {
                                console.log(authData)
                                $scope.authData = authData;
                            }).
                        catch (function(error) {
                            $scope.error = error;
                        });
                    };



                    $scope.alseCards = $firebaseArrary(ref.child("alseCards"));
                    $scope.items = $firebaseArrary(ref.child("items"));
                }
            ]);
    </script>
</body>

安全和规则.json

 {
  "rules": {
    "users": {"$user_id": {".write": "$user_id === auth.uid", ".read": "$user_id === auth.uid"}},
    "items": {".write": "auth !== null && auth.provider === 'password'", ".read": "auth !== null && auth.provider === 'password'"},
    "alseCards": {".write": "auth !== null && auth.provider === 'password'", ".read": "auth !== null && auth.provider === 'password'"}
  }
}

提前谢谢。

4

1 回答 1

1

我认为问题在于您有代码可以在登录功能之外获取您的物品和 alseCards。这意味着它在第一次调用控制器并且用户尚未登录时执行。尝试将它放在登录函数中,如下所示:

$scope.login = function() {
  $scope.authData = null;
  $scope.error = null;

  auth.$authWithPassword({
    email: "email@myemail.com",
    password: "alseTest"
  }).then(function(authData) {
    console.log(authData)
    $scope.authData = authData;
    $scope.alseCards = $firebaseArrary(ref.child("alseCards"));
    $scope.items = $firebaseArrary(ref.child("items"));
  }).
  catch(function(error) {
    $scope.error = error;
  });
};
于 2015-07-22T07:12:48.940 回答