persistent authentication states Firebase

浪尽此生 提交于 2019-12-12 04:18:52

问题


Lets say I've configured my write rules to a specific email :

{
  "rules": {
    ".read": true,
    ".write": "auth.email == 'example@example.com'"
  }
}

And I login in using the angularFireAuth service:

.controller('loginCtrl', ["$scope", "$rootScope", "angularFireAuth", function($scope, $rootScope, angularFireAuth) {

    var ref = new Firebase("https://test.firebaseio.com/");
    angularFireAuth.initialize(ref, {scope: $scope, name: "user"});

    $scope.cred = {};

    $scope.signin = function() {

        angularFireAuth.login('password', {
            email: $scope.cred.user,
            password: $scope.cred.password,
            rememberMe: true
        });
    }
}]);

I have a newCtrl that I use to add new items to my collection:

.controller('newCtrl', ["$scope", "$rootScope", "angularFireCollection", function($scope, $rootScope, angularFireCollection) {

    var ref = new Firebase("https://test.firebaseio.com/");
    $scope.items = angularFireCollection(ref);
}]);

When I invoke items.add() in my view:

<input type="text" ng-model="item.name" />
<input type="text" ng-model="item.desc" />
<button ng-click="items.add(item)">submit</button>

I recieve a Not Authorized response even after I've successfully logged in through the loginCtrl

  • How do I create a persistent authentication state across my entire application after logging in?
  • How do I remove a persistent authentication state across my entire application after logging out?

回答1:


Your authentication state is session-based, not dependent on your controller scope, so you already have a "persistent authentication state" across your application.

There must be something else going on with your code that is generating that error, but as far as your question goes, you can see a working example here using example@example.com as the email and example as the password.

I matched your security rules as well, so if you login instead as test@example.com with password test you won't be able to create items.




回答2:


From what I can tell angularFireAuth broadcasts the login events from the $rootscope. You can set things to happen .$on('angularFireAuth:login',function(event){...}), or .$on('angularFireAuth:logout',function(event){...}), or .$on('angularFireAuth:error',function(event,error){...}). With this in mind you may want something like:

.controller('newCtrl', ["$scope", "$rootScope", "angularFireCollection", function($scope, $rootScope, angularFireCollection) {
    var ref = new Firebase("https://test.firebaseio.com/");
    $scope.items = angularFireCollection(ref);
    $scope.$on('angularFireAuth:login', function(){
        $scope.addItem=function(item){
            $scope.items.add(item);
        };
    });
}]);

.. Which should ensure that the user authentication event occurs before the function is assigned to the $scope.



来源:https://stackoverflow.com/questions/19821571/persistent-authentication-states-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!