Pregunta

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?
¿Fue útil?

Solución

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.

Otros consejos

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top