0

I have this code inside of a file called Auth.js . hasRole: function(){ console.log(currentUser); return currentUser.role; //role is an array of objects but gives undefined }

I need to resolve the value of hasRole before redirecting anywhere just after login.

login looks like this:

 var role=[];

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();
          console.log(currentUser);//gives an unresolved promise (output is given after this piece of code.

           role = currentUser.role;  //gives undefined 
          deferred.resolve(data);    
          return cb();
        }).
        error(function(err) {
          this.logout();
          deferred.reject(err);
          return cb(err);
        }.bind(this));

        return deferred.promise;
      }}

and is called as follows:

 $scope.login = function(form) {
      $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) //gives undefined.
        //Need to redirect on basis of value of role
        /*if(role.priority >= 1){
          $location.path('/admincontrol');

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

        }*/


        })
        .catch( function(err) {
          $scope.errors.other = err.message;
        });
      }
    };

How do I access currentUser.role here? I need to have its value in $scope.login just after login and just before redirecting so that I can redirect on basis of its value.

Edit: User service looks like this:

'use strict';

angular.module('createProjectApp')
  .factory('User', function ($resource) {
  //  return $resource('/api/users/:id/:controller', {
    return $resource('/api/customUsers/:id/:controller', {
      id: '@_id'
    },
    {
      changePassword: {
        method: 'PUT',
        params: {
          controller:'password'
        }
      },
      get: {
        method: 'GET',
        params: {
          id:'me'
        }
      }
      });
  });

currentUser when consoled in login looked like this: enter image description here

8
  • If User.get() returns a promise why do you expect role to be property of the promise? You already have usage of promises in your code, so you're supposed to understand that value will be resolved later and should be accessed in .then Commented Aug 5, 2015 at 22:18
  • @KirillSlatin: I am a new bie in angular and have started using angular fullstack by yeoman. so I was tryign to integrate pieces of code here and there. I am yet not very clear about the idea of promises.. It would be really helpful if you could please throw some light on it. Commented Aug 5, 2015 at 22:24
  • docs.angularjs.org/api/ng/service/$q light thrown. Alternatively; Google "angular promise" Commented Aug 5, 2015 at 22:49
  • That's kind but I actually meant with the case that I have since I had trouble understanding why currentUser.then was not a function. Commented Aug 5, 2015 at 22:51
  • Have you tried currentUser.then? If currentUser is a promise then SHOULD be a method on it. Commented Aug 5, 2015 at 22:55

1 Answer 1

1

User.get() is itself a function that returns a promise. You are going to have to wait on the promise fullfillment before continuing, as in

currentUser.then(function() {
  console.log(currentUser);
  role = currentUser.role;  //gives undefined 
  deferred.resolve(data);    
  return cb();
}

cb() itself is not needed since you are using promises - but you may have something special in mind.

Sign up to request clarification or add additional context in comments.

10 Comments

It gives an error that says currentUser.then is not a function
@SimranKaur can share User.get implementation then? Why do you think that it returns a promise?
@KirillSlatin: I thought so because when I did console.log(currentUser) inside of login as mentioned above in the code, I got console output as given in the picture which I thought should suggest it's a promise?
@SimranKaur It's absolutely not obvious what the picture with log stands for. Looks like it's a result of $resource call. In my project I have different usage. Not attached .then but a callback provided to the method. It expect it to work User.get(function(){...}) with the function that Paul wrote
@SimranKaur well it's hard to do without seeing code of Auth.hasRole()
|

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.