I am using AngularJs and Ui-Router and I am trying to set two different home pages, one for users that are logged in and the other for users that aren't. But I am getting the following error:
RangeError: Maximum call stack size exceeded
I ran console.trace()
and I can see that there is an issue which is causing the states to loop infinitely (or something like that). BUt I have no idea how to fix it.
Here is the code that is generating the error.
.run(function ($rootScope, $state, $location, Auth) {
$rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState) {
if(fromState.name === "") {
if (Auth.isLoggedIn()) {
$state.go('main');
event.preventDefault();
} else {
$state.go('welcome');
event.preventDefault();
}
} else {
if (toState.authenticate && !Auth.isLoggedIn()) {
$location.path('/login');
event.preventDefault();
}
}
});
From what I can tell it seems to stem from if(fromState.name === "")
I've created an example, playing with default pages and auth/unauth user. Similar issue could be seen here
Firstly this would be the listener:
What is happening?
And here are states:
Check plunker here or here
Use $state.go("main", {}, {notify:false}); for not notify to "$stateChangeStart"event.