Angular JS $locationChangeStart get next url route object

后端 未结 4 2088
忘掉有多难
忘掉有多难 2021-02-14 08:44

I am trying to implement Authorization on my angular application, when a route is changed I want to check whether the route is authorized for user or not. I tried with $ro

4条回答
  •  甜味超标
    2021-02-14 09:04

        $routeProvider
            .when('/', {
                title: 'Home',
                templateUrl: 'partials/home',
                controller: 'HomeController',
                access: {
                    isFree: true
                }
            })
            .when('/about-us', {
                title: 'About us',
                templateUrl: 'partials/aboutus',
                controller: 'AboutUsController',
                access: {
                    isFree: true
                }
            })
            .when('/how-it-works', {
                title: 'How It Works',
                templateUrl: 'partials/howitworks',
                controller: 'HowItWorksController',
                access: {
                    isFree: true
                }
            })
            .when('/login', {
                templateUrl: 'users/login',
                controller: 'LoginController',
                access: {
                    isFree: true
                }
            })
            .when('/logout', {
                controller: 'LogoutController',
                access: {
                    isFree: false
                }
            })
            .when('/sign-up', {
                templateUrl: 'users/signup',
                controller: 'SignUpController',
                access: {
                    isFree: true
                }
            })
            .otherwise({
                redirectTo: '/'
            });
    })
    
    
    .run(['$rootScope', '$location','$log','$window','Auth' ,function($rootScope, $location, $log, $window, Auth) {
    
        $rootScope.$on('$routeChangeStart', function(event, currRoute, prevRoute){
            $rootScope.title = '';
            if(currRoute.$$route.title !== undefined){
                $rootScope.title = currRoute.$$route.title ;
            }
          //  $rootScope.userLoggedIn = {name : 'Hi, '+ 'Amar'}    
    
            let checkIsLoggedInForRoute = ['/login','/sign-up'];
            let isFreeAccess = currRoute.$$route.access.isFree;
            let isLoggedIn = Auth.isLogin();
    
            if(isFreeAccess){
                if(checkIsLoggedInForRoute.indexOf($location.path()) !== -1 && isLoggedIn){
                    event.preventDefault();
                    $location.path('/')   
                }
            }else if(!isFreeAccess){
                let isLogoutRoute = currRoute.$$route.originalPath.indexOf('/logout') !== -1;
                if(isLogoutRoute && isLoggedIn){
                    Auth.logout();           
                    $location.path('/');    
                }else if(isLogoutRoute && !isLoggedIn){ 
                    $location.path('/login');
                } 
            }
        });
    }]);
    

提交回复
热议问题