1

I have these functions:

getPermissions: function(){
        return currentUser.permissions;
       }, //It returns permissions which is an array of strings as well.I use it in resolve first, before loading the page and it works

hasRole: function(){
        console.log(currentUser);
      return currentUser.role; //role is an array of objects but gives undefined
       }

currentUser is an object that looks like this:

currentUser

currentUser.role gives undefined probably because promise has not been resolved yet. I have to redirect the user just after login on basis of value contained in currentUser.role. How do I ? first resolve and then redirect? I think it really should solve the problem.

EDIT: I have above mentioned functions in a file called auth.js. auth.js has login function as well that looks like this:

 login: function(user, callback) {
        var cb = callback || angular.noop;
        var deferred = $q.defer();

        $http.post('/auth/local', {
          email: user.email,
          password: user.password
        }).
        success(function(data) {
          $cookieStore.put('token', data.token);
          currentUser = User.get();
          deferred.resolve(data);
          return cb();
        }).
        error(function(err) {
          this.logout();
          deferred.reject(err);
          return cb(err);
        }.bind(this));

        return deferred.promise;
      }

I am calling login function from auth.js in login.controller.js.

  $scope.submitted = true;

      if(form.$valid) {
        Auth.login({
          email: $scope.user.email,
          password: $scope.user.password
        })
        .then( function() {
          // Logged in, redirect to home
        var role = Auth.hasRole();
        console.log("role");
        console.log(role);  //gives undefined
       if(role.priority >= 1){
          $location.path('/admincontrol');

        }else{
          $location.path('/');

        }

        })
        .catch( function(err) {
          $scope.errors.other = err.message;
        });
      }
    };
5
  • Yes, It gives can not read property 0 of undefined. Commented Aug 5, 2015 at 13:32
  • Could you add where and how you initialize the "currentUser" var ? Commented Aug 5, 2015 at 14:03
  • 2
    Are you sure, that currentUser initialized before you're trying to get access to role. Chrome lists object only after you're expanding it. Please provide part of code, which should initilalize currentUser Commented Aug 5, 2015 at 14:39
  • @Andrey: Could you please check the update ? Commented Aug 5, 2015 at 21:18
  • @SimranKaur: And what User.get() returns? I can only guess that this is a promise. Commented Aug 6, 2015 at 12:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.