身份验证检查角度2

时间:2017-08-31 19:45:39

标签: angular

检查角度2中的身份验证状态的最佳方法是什么?在角度1中,使用.run方法,验证令牌在会话/本地存储中是直截了当的,如果令牌不存在则意味着用户未连接,这是我如何做的:

function authenticationCheck(HOME_PAGE, $http, $localStorage, $location, $rootScope, $sessionStorage) {
// keep user logged in after page refresh
if ($localStorage.token) {
  $http.defaults.headers.common.Authorization = $localStorage.jwt;
}

// redirect to login page if not logged in and trying to access a restricted page
$rootScope.$on('$locationChangeStart', function (event, next, current) {
  var publicPages = ['/login'];
  var restrictedPage = publicPages.indexOf($location.path()) === -1;
  if (restrictedPage && !($localStorage.jwt || $sessionStorage.jwt)) { // redirect to login
    $location.path('/login');
  }
  if (($localStorage.jwt || $sessionStorage.jwt) && $location.path() === '/login') { // redirect to home page if connected
    $location.path(HOME_PAGE);
  }
});
}

我想使用等效的角度2,似乎没有.run或.config方法。

0 个答案:

没有答案