I want my application to redirect non-logged in users to a login page. Following advice from a popular source, the app listens for routeChangeStart events like this:
$rootScope.$on("$routeChangeStart", function(event, next, current) {
if ($rootScope.currentUser === null) {
var allowPublic = ["partials/logIn.html", "partials/start.html"];
var allowed = _.contains(allowPublic, next.templateUrl);
if (!allowed) {
$location.path("/logIn").replace();
}
}
});
Although this logic functions correctly, changing $location.path does not seem to have any effect. The URL changes to /logIn in the browser's address bar, but the view still displays the unauthorized content. Any ideas on what might be causing this issue?
My routes are defined as follows:
$routeProvider.when('/start', {templateUrl: 'partials/start.html', controller: 'StartController'});
$routeProvider.when('/logIn', {templateUrl: 'partials/logIn.html', controller: 'LogInController'});
$routeProvider.when('/restricted', {templateUrl: 'partials/restricted.html', controller: 'RestrictedController'});
In certain instances, the start page controller attempts to navigate to the restricted page like so:
// within StartController
$scope.pressedButton = function() {
$location.path('/restricted');
// I would like to handle this and redirect based on previous logic if user is not logged in
};