angular.module('myApp')
         .controller('AppCtrl', function($scope, $rootScope, $state, $sessionStorage, Auth) {
            $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){
           // checking token is present or not
            if( !Auth.isLoggedIn() ) {
                event.preventDefault();
                $state.go('access.login');// go to login page, token not present.
           }
            if(toState.name == 'access.login' && Auth.isLoggedIn())        	{
                $state.go('dashboard');// go to dashboard page, if token is present
            }
        });

       $scope.logout = function() {
            Auth.logout();// remove the token.
            $state.go('access.login'); // go to the login page
       }
});

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

  1. https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js