So I'm doing a simple 'is the user logged in' check and redirecting the user to the signin.html page if they are not logged in. My code looks something like this:
var myAppModule = angular.module('myApp', ['ngRoute', 'ngCookies']);
myAppModule.config(function($routeProvider) {
$routeProvider
.when('/page2', {
controller:'pagetwo',
templateUrl:'pagetwo.html'
})
.when('/signin', {
controller:'signinCTRL',
templateUrl:'signin.html'
})
});
myAppModule.run(function($rootScope, userInfo, $location) {
$rootScope.$on('$routeChangeSuccess', function () {
myCurrentRoute = $location.$$path;
if(myCurrentRoute != "/signin") {
userInfo.checkLogin(); // Check login first. This has the $location.path( "/signin" ); in it.
}
})
});
My problem is that when a user goes to /page2, the $routeChangeSuccess function fires fine and inside the userInfo.checkLogin() I redirect them to a new route that redirects them on the signin.html page.
The problem is that the pagetwo controller still fires before the user is redirected to the signinCTRL controller.
Is there a way to prevent the pagetwo controller from running somehow? I know I could probably just wrap everything in the signinCTRL to check if the user is logged in, but that defeats the purpose of having the routeChangeSuccess function.