2016-06-15 57 views
0

我有一個簡單的jijster應用程序。我希望經過身份驗證的用戶重定向到無效的主頁url。此前我已經改變home.state.js並添加login.state.js提到如下:將已驗證身份的用戶重定向到無效網址匹配的主頁

home.state.js

(function() { 
    'use strict'; 

    angular 
     .module('anvilIqApp') 
     .config(stateConfig); 

    stateConfig.$inject = ['$stateProvider']; 

    function stateConfig($stateProvider) { 
     $stateProvider.state('home', { 
      parent: 'app', 
      url: '/home', 
      data: { 
       authorities: ['ROLE_USER'] 
      }, 
      views: { 
       '[email protected]': { 
        templateUrl: 'app/home/home.html', 
        controller: 'HomeController', 
        controllerAs: 'vm' 
       } 
      }, 
      resolve: { 
       mainTranslatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate,$translatePartialLoader) { 
        $translatePartialLoader.addPart('home'); 
        return $translate.refresh(); 
       }] 
      } 
     }); 
    } 
})(); 

login.state.js

(function() { 
    'use strict'; 

    angular 
     .module('anvilIqApp') 
     .config(stateConfig); 

    stateConfig.$inject = ['$stateProvider']; 

    function stateConfig($stateProvider) { 
     $stateProvider.state('login', { 
      parent: 'app', 
      url: '/', 
      data: { 
       authorities: [] 
      }, 
      views: { 
       '[email protected]': { 
        templateUrl: 'app/components/login/login.html', 
        controller: 'LoginController', 
        controllerAs: 'vm' 
       } 
      }, 
      resolve: { 
       mainTranslatePartialLoader: ['$translate', '$translatePartialLoader', function ($translate,$translatePartialLoader) { 
        $translatePartialLoader.addPart('login'); 
        return $translate.refresh(); 
       }] 
      } 
     }); 
    } 
})(); 

正如你所看到的,根目前是登錄頁面。當通過身份驗證的用戶遇到無效url時,將會像往常一樣顯示登錄頁面。我想將用戶重定向到/home以尋找無效的URL。

auth.service.js我有這個片段:

function authThen() { 
    var isAuthenticated = Principal.isAuthenticated(); 

    // an authenticated user can't access to login and register pages 
    if (isAuthenticated && $rootScope.toState.parent === 'account' && ($rootScope.toState.name === 'login' || $rootScope.toState.name === 'register')) { 
     $state.go('home'); 
    } 

    // recover and clear previousState after external login redirect (e.g. oauth2) 
    if (isAuthenticated && !$rootScope.fromState.name && getPreviousState()) { 
     var previousState = getPreviousState(); 
     resetPreviousState(); 
     $state.go(previousState.name, previousState.params); 
    } 

    if ($rootScope.toState.data.authorities && $rootScope.toState.data.authorities.length > 0 && !Principal.hasAnyAuthority($rootScope.toState.data.authorities)) { 
     if (isAuthenticated) { 
      // user is signed in but not authorized for desired state 
      $state.go('accessdenied'); 
     } 
     else { 
      // user is not authenticated. stow the state they wanted before you 
      // send them to the login service, so you can return them when you're done 
      storePreviousState($rootScope.toState.name, $rootScope.toStateParams); 

      // now, send them to the signin state so they can log in 
      $state.go('accessdenied').then(function() { 
       $state.go('login'); 
      }); 
     } 
    } 
} 

回答

相關問題