I am working on an Angularjs SPA with angular-ui-route v0.3.1.
Recently applied rule() to allow case insensitive urls...
$urlRouterProvider.rule(function ($injector, $location) {
//what this function returns will be set as the $location.url
var path = $location.path(), normalized = path.toLowerCase();
if (path != normalized) {
//instead of returning a new url string, I'll just change the $location.path directly so I don't have to worry about constructing a new url string and so a new state change is not triggered
$location.replace().path(normalized);
}
// because we've returned nothing, no state change occurs
});
also i am listening to $stateChangeStart
event to check if the user is not logged in and redirect him to login page
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState, fromParams) {
if (!('data' in toState) || !('access' in toState.data)) {
messageHandler.show({
message: 'Access undefined for this state',
messageType: messageHandler.messageTypes.error
});
event.preventDefault();
}
else
if (!auth.authorize(toState.data.access)) {
event.preventDefault();// this line cause my circular issue
if (!auth.isLoggedIn()) {
var redirectState = toState.name;
auth.login( redirectState);
}
else {
messageHandler.show({
message: 'Seems like you tried accessing a route you do not have access to : ' + toState.name,
messageType: messageHandler.messageTypes.warning
});
$state.go('unauthorized');
}
}
});
The problem is that the role filter is do normalize the url, them when doing event.preventDefault() in $stateChangeStart
event, the normalized url is being cancelled and the loop occurs again.
any help will be appreciated.