2015-10-16 86 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