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.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;
});
}
};
currentUser
initialized before you're trying to get access torole
. Chrome lists object only after you're expanding it. Please provide part of code, which should initilalizecurrentUser
– Andrey Aug 5 at 14:39User.get()
returns? I can only guess that this is a promise. – Andrey Aug 6 at 12:24