0

In my app, I require to check the user details, in case if the user is not logged in, then i require to redirect back to /login page. but my try is not working, where should I do this config?

my try :

(function(){

    "use strict";

    angular.module('meanOffice')
        .config(routeConfig)
        .run(stateController);


    function routeConfig( $locationProvider, $stateProvider, $urlRouterProvider ){

        $stateProvider
            .state('login', {
                url:'/',
                templateUrl : 'app/views/pages/login/login.html',
                controller  : 'mainController as main'
            })
            .state('users', {
                url:'/users',
                templateUrl : 'app/views/pages/users/users.html',
                controller  : 'usersController as users',
                required    : true,
                resolve     : {
                    usersData : function(){

                        return "Arif";
                    }
                }
            })


        $urlRouterProvider.otherwise('/');

    }


    function stateController( $rootScope, auth, authToken ){

        $rootScope.$on('$routeChangeStart', function(){

            var isLoggedIn = auth.isLoggedIn();
//but each time the route change, this is not called..!?
            if( !isLoggedIn )
                $location.path('/');

        });


    }

})();
user2024080
  • 1
  • 14
  • 56
  • 96
  • getting any error??what should auth.isLoggedIn() returns?? – Sa E Chowdary Nov 17 '16 at 10:43
  • it returns true or false according to the value. but even i am not getting triggered the `routeChageStart` at all, on each time – user2024080 Nov 17 '16 at 10:46
  • okay are you sure that the state Controller function is not calling??if your sure try to make this Run method approach mentioned in the accepted answer http://stackoverflow.com/questions/20969835/angularjs-login-and-authentication-in-each-route-and-controller – Sa E Chowdary Nov 17 '16 at 10:50

1 Answers1

0

This is my fault, if i use $stateChageStart - it works fine.

here is my update

$rootScope.$on("$stateChangeStart", function(){

    console.log('each time starts')

})
user2024080
  • 1
  • 14
  • 56
  • 96