Skip to main content
adicionou 80 caracteres ao conteúdo
Fonte Link

Só que quando uso o $state.go('app.home'); ele entra em loop infinito.

O meu código é o seguinte:

O meu código é o seguinte:

Só que quando uso o $state.go('app.home'); ele entra em loop infinito.

O meu código é o seguinte:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { if (typeof(toState) !== undefined){ $templateCache.remove(toState.templateUrl); }
  if($rootScope.session == undefined && $cookieStore.get('user') == undefined) 
  {
    $rootScope.session =  {};
  }else if($rootScope.session == undefined && $cookieStore.get('user') != undefined) 
  {   

      $rootScope.session = {
        set:true, 
        name : $cookieStore.get('user')['name'], 
        userid : $cookieStore.get('user')['id'], 
        role: $cookieStore.get('user')['nivel'],
        email: $cookieStore.get('user')['email']
      };          
  }

  if(toState.name === "page.login" || toState.name === "page.register")
  {
    return;
  }

  var authorized = true;

  if(Object.keys($rootScope.session).length === 0) 
  {
      event.preventDefault();
      $state.go('page.login');
      return;
  }else if(Object.keys(toState.permissions).length !== 0) {
    
    angular.forEach(toState.permissions, function(value, key)
    {
      angular.forEach(value, function(role) 
      {            
        if(key === 'except' && role === $rootScope.session.role)
        {
          authorized = false;
        }else if(key === 'allow' && role !== $rootScope.session.role)
        {
          authorized = false;
        }; 
      });  
    });
  }

  if(!authorized){
    event.preventDefault();
    $state.go('app.home');
    return;
  }; 

}); // Start View

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams)
   {      
      if (typeof(toState) !== undefined){
        $templateCache.remove(toState.templateUrl);
      }

      if($rootScope.session == undefined && $cookieStore.get('user') == undefined) 
      {
        $rootScope.session =  {};
      }else if($rootScope.session == undefined && $cookieStore.get('user') != undefined) 
      {   

          $rootScope.session = {
            set:true, 
            name : $cookieStore.get('user')['name'], 
            userid : $cookieStore.get('user')['id'], 
            role: $cookieStore.get('user')['nivel'],
            email: $cookieStore.get('user')['email']
          };          
      }

      if(toState.name === "page.login" || toState.name === "page.register")
      {
        return;
      }

      var authorized = true;

      if(Object.keys($rootScope.session).length === 0) 
      {
          event.preventDefault();
          $state.go('page.login');
          return;
      }else if(Object.keys(toState.permissions).length !== 0) {
        
        angular.forEach(toState.permissions, function(value, key)
        {
          angular.forEach(value, function(role) 
          {            
            if(key === 'except' && role === $rootScope.session.role)
            {
              authorized = false;
            }else if(key === 'allow' && role !== $rootScope.session.role)
            {
              authorized = false;
            }; 
          });  
        });
      }

      if(!authorized){
        event.preventDefault();
        $state.go('app.home');
        return;
      }; 

   }); // Start View

Tentei também: var array = toState.permissions['except'];

if(array.indexOf($rootScope.session.role) > 0) { authorized = false; }

var array = toState.permissions['except'];

if(array.indexOf($rootScope.session.role) > 0)
{
  authorized = false;
}
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { if (typeof(toState) !== undefined){ $templateCache.remove(toState.templateUrl); }
  if($rootScope.session == undefined && $cookieStore.get('user') == undefined) 
  {
    $rootScope.session =  {};
  }else if($rootScope.session == undefined && $cookieStore.get('user') != undefined) 
  {   

      $rootScope.session = {
        set:true, 
        name : $cookieStore.get('user')['name'], 
        userid : $cookieStore.get('user')['id'], 
        role: $cookieStore.get('user')['nivel'],
        email: $cookieStore.get('user')['email']
      };          
  }

  if(toState.name === "page.login" || toState.name === "page.register")
  {
    return;
  }

  var authorized = true;

  if(Object.keys($rootScope.session).length === 0) 
  {
      event.preventDefault();
      $state.go('page.login');
      return;
  }else if(Object.keys(toState.permissions).length !== 0) {
    
    angular.forEach(toState.permissions, function(value, key)
    {
      angular.forEach(value, function(role) 
      {            
        if(key === 'except' && role === $rootScope.session.role)
        {
          authorized = false;
        }else if(key === 'allow' && role !== $rootScope.session.role)
        {
          authorized = false;
        }; 
      });  
    });
  }

  if(!authorized){
    event.preventDefault();
    $state.go('app.home');
    return;
  }; 

}); // Start View

Tentei também: var array = toState.permissions['except'];

if(array.indexOf($rootScope.session.role) > 0) { authorized = false; }

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams)
   {      
      if (typeof(toState) !== undefined){
        $templateCache.remove(toState.templateUrl);
      }

      if($rootScope.session == undefined && $cookieStore.get('user') == undefined) 
      {
        $rootScope.session =  {};
      }else if($rootScope.session == undefined && $cookieStore.get('user') != undefined) 
      {   

          $rootScope.session = {
            set:true, 
            name : $cookieStore.get('user')['name'], 
            userid : $cookieStore.get('user')['id'], 
            role: $cookieStore.get('user')['nivel'],
            email: $cookieStore.get('user')['email']
          };          
      }

      if(toState.name === "page.login" || toState.name === "page.register")
      {
        return;
      }

      var authorized = true;

      if(Object.keys($rootScope.session).length === 0) 
      {
          event.preventDefault();
          $state.go('page.login');
          return;
      }else if(Object.keys(toState.permissions).length !== 0) {
        
        angular.forEach(toState.permissions, function(value, key)
        {
          angular.forEach(value, function(role) 
          {            
            if(key === 'except' && role === $rootScope.session.role)
            {
              authorized = false;
            }else if(key === 'allow' && role !== $rootScope.session.role)
            {
              authorized = false;
            }; 
          });  
        });
      }

      if(!authorized){
        event.preventDefault();
        $state.go('app.home');
        return;
      }; 

   }); // Start View

Tentei também:

var array = toState.permissions['except'];

if(array.indexOf($rootScope.session.role) > 0)
{
  authorized = false;
}
Fonte Link

Problema de loop com $stateChangeStart

Olá pessoa estou criando um sistema de autenticação e verificação de niveis com Angularjs, estou usando o stateChangeStart para validar as permissões em view.

O meu código é o seguinte:

$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) { if (typeof(toState) !== undefined){ $templateCache.remove(toState.templateUrl); }
  if($rootScope.session == undefined && $cookieStore.get('user') == undefined) 
  {
    $rootScope.session =  {};
  }else if($rootScope.session == undefined && $cookieStore.get('user') != undefined) 
  {   

      $rootScope.session = {
        set:true, 
        name : $cookieStore.get('user')['name'], 
        userid : $cookieStore.get('user')['id'], 
        role: $cookieStore.get('user')['nivel'],
        email: $cookieStore.get('user')['email']
      };          
  }

  if(toState.name === "page.login" || toState.name === "page.register")
  {
    return;
  }

  var authorized = true;

  if(Object.keys($rootScope.session).length === 0) 
  {
      event.preventDefault();
      $state.go('page.login');
      return;
  }else if(Object.keys(toState.permissions).length !== 0) {
    
    angular.forEach(toState.permissions, function(value, key)
    {
      angular.forEach(value, function(role) 
      {            
        if(key === 'except' && role === $rootScope.session.role)
        {
          authorized = false;
        }else if(key === 'allow' && role !== $rootScope.session.role)
        {
          authorized = false;
        }; 
      });  
    });
  }

  if(!authorized){
    event.preventDefault();
    $state.go('app.home');
    return;
  }; 

}); // Start View

Tentei também: var array = toState.permissions['except'];

if(array.indexOf($rootScope.session.role) > 0) { authorized = false; }

Obrigado.