2015-10-16 88 views
0

我已经下载了yeoman angular fullstack,它带有登录的基本配置。登录时检查角色

的代码是这样的:

$scope.login = function(form) { 
    $scope.submitted = true; 
    if(form.$valid) { 
    Auth.login({ 
     email: $scope.user.email, 
     password: $scope.user.password 
    }) 
    .then(function() { 
    $scope.users = User.query(); 
     // Logged in, redirect to home 
    $state.go('backend', {}); 
    }) 
    .catch(function(err) { 
     $scope.errors.other = err.message; 
    }); 
    } 
}; 

我要的是在登录过程中检查的作用和重定向他视它。我试着用这段代码,但总是返回false。

  if(Auth.isAdmin()){ 
     $state.go('backend', {}); 
     }else{ 
     $state.go('backend', {}); 
     } 

所以,我有一个问题:

1这是什么地方存储在用户对象在登录后的客户端?如果发生在哪里?它具有所有的性能?

回答

0

您可以对您的问题here

请注找到答案,那currentUser不会立即设置。所以这里有两种挣扎的方式。在我的项目中,我们也使用第一个:

if(form.$valid) { 
    Auth.login({ 
     email: $scope.user.email, 
     password: $scope.user.password 
    }) 
    .then(function() { 
     // need to wait until currentUser will set 
     Auth.isLoggedInAsync(function(success) { 
     var role = Auth.getCurrentUser().role 
     console.log("Role of user is: " + role); 
     if(role == 'admin'){ 
      $state.go('adminpage', {}); 
     }else{ 
      $state.go('userpage', {}); 
     } 
     }); 
    }) 
    .catch(function(err) { 
     $scope.errors.other = err.message; 
    }); 
    } 
}; 
+0

哦,非常感谢,那是我的错误。固定。我采取了第二种选择,因为我认为这对长期来说更好。 – ssg