使用Angular.js和$ .authwithPassword()

时间:2015-07-22 03:37:54

标签: firebase firebase-security firebase-authentication

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

的index.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>

安全和Rules.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'"}
  }
}

提前致谢。

1 个答案:

答案 0 :(得分:1)

我认为问题是你有代码来获取登录功能之外的你的物品和alseCards。这意味着它在首次调用控制器时执行,此时用户还没有登录。尝试将它放在登录函数中,如下所示:

1 1000000