How To Redirect User To Login Page When Refreshing The Page In Angularjs
In my application I want to redirect the user to the login page when he refreshes the page he is viewing. How can I do this in AngularJS?
Solution 1:
You may listen to event $locationChangeStarted and, if user not logged in, prevent default behavior (preventDefault() method of event object) and change $location to any page you want.
Here is a pseudocoded example:
myModule.run(function($locaiton, $rootScope, MyLoginService) {
   $rootScope.on('$locationChangeStarted', function(event) {
       if (!MyLoginService.isAuthentificated() && event.oldUrl !== 'myloginpage') {
          event.preventDefault();
          $location.path('/myloginpage');
       }
   });
});
Post a Comment for "How To Redirect User To Login Page When Refreshing The Page In Angularjs"