繁体   English   中英

Angularjs登录身份验证:阻止用户导航到除登录页面和注册页面以外的其他页面

[英]Angularjs Login Authentication: Prevent user navigate to other page except login page and registration page

我正在通过使用AngularJS框架开发Web应用程序。 对于我的登录页面,我必须阻止用户浏览到除登录页面和注册之外的其他页面。 但是我现在所做的代码,也阻止了用户导航到注册页面。 以下是我的代码。 我如何解决此问题,以便仅当用户未登录时才允许用户浏览登录页面和注册页面。

.run(function ($rootScope, $state, AuthService, AUTH_EVENTS) {
  $rootScope.$on('$stateChangeStart', function (event,next, nextParams, fromState) {

    if ('data' in next && 'authorizedRoles' in next.data) {
      var authorizedRoles = next.data.authorizedRoles;
      if (!AuthService.isAuthorized(authorizedRoles)) {
        event.preventDefault();
        $state.go($state.current, {}, {reload: true});
        $rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
      }
    }

    if (!AuthService.isAuthenticated()) {
      if (next.name !== 'login') {
        event.preventDefault();
        $state.go('login');
      }
    }
  });

您可以通过添加在数据属性一个布尔参数实现这一目标.state ,让说requiresAuth和检查也.RUN块;

下面是伪代码

.config块中

 $stateProvider
  .state("register", {
        url: '/register',
        templateUrl: 'register.html',
        controller:'UserController',
        controllerAs: 'vm',
        data: {
            requiresAuth: false,
            pageTitle: 'Register'                
        }
 })
 .state("dashboard", {
        url: '/dashboard',
        templateUrl: 'dashboard.html',
        controller:'OtherController',
        controllerAs: 'vm',
        data: {
            requiresAuth: true,
            pageTitle: 'Dashboard',
            authorizedRoles: ['WHATEVER_ROLE']
        }
});

并在.run块中

var stateChangeStart = $rootScope.$on('$stateChangeStart', function(event, toState, toParams) {
    if (AuthService.isAuthenticated()) {
        // if user trying to access register/forgot page after login than redirect to dashboard
        if (!toState.data.requiresAuth) {
            event.preventDefault();
            $rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
        }
        // user is not authenticated and trying to access page which is not permissible than send back to dashboard
        if (angular.isDefined(toState.data.authorizedRoles)) {
            var roles = toState.data.authorizedRoles;
            AuthService.isAuthorized(roles).catch(function() { // NOTE: here we are only handling with .catch block
                event.preventDefault();
                $rootScope.$broadcast(AUTH_EVENTS.notAuthorized);
            });
        }
    }
    // user is not authenticated than redirect to login
    else if (toState.data.requiresAuth) {
        event.preventDefault();
        $rootScope.$broadcast(AUTH_EVENTS.notAuthenticated);
    }
});

 var notAuthenticated = $rootScope.$on(AUTH_EVENTS.notAuthenticated, function() {
        $log.warn('not authenticated');
        $state.go('login', null, {});
        return;
    });

    var notAuthorized = $rootScope.$on(AUTH_EVENTS.notAuthorized, function() {
        $log.warn('not authorized');
        $state.go('dashboard');
        return;
    });

    // DO NOT forget to destroy 
    $rootScope.$on('$destroy', notAuthenticated);
    $rootScope.$on('$destroy', notAuthorized);
    $rootScope.$on('$destroy', stateChangeStart);

暂无
暂无

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

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