我有这些功能:
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
是一个看起来像这样的对象:
currentUser.role
可能未定义,因为promise还没有解决。我必须在登录后根据currentUser.role中包含的值重定向用户。我如何能 ?先解决然后重定向?我认为它确实应该解决问题。
编辑:我在名为auth.js
的文件中有上述功能。 auth.js
也有登录功能,如下所示:
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;
}
我正在auth.js
中的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;
});
}
};