简体   繁体   English

登录时检查角色

[英]Check role on login

I have downloaded the yeoman angular fullstack, it comes with the basic configuration for the login. 我已经下载了yeoman angular fullstack,它带有登录的基本配置。

The code is like: 代码如下:

$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;
    });
  }
};

What I want is check the role during the login and redirect him depending of it. 我想要的是在登录期间检查角色,然后根据需要重定向他。 I have try with this piece of code but allways return false. 我已经尝试过这段代码,但始终返回false。

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

So, I have a question: 所以,我有一个问题:

1- It's is stored the user object somewhere in the client side after login? 1-登录后将用户对象存储在客户端的某个位置吗? In case afirmative Where? 万一肯定在哪里? It have all the atruibutes? 它有所有的美味佳肴吗?

You can find answer on your question here 您可以在这里找到问题的答案

Please, note, that currentUser is not set immediately. 请注意,currentUser不会立即设置。 So here is two ways of struggling with that. 因此,有两种解决方法。 In my project we're also using the first one: 在我的项目中,我们还使用第一个:

  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;
    });
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM