My app have states that require authentication. When user is logged in and opens an application - I want to skip default ‘login’ page and want to transition to the ‘user home’ page.
So in .run() function I have the following code:
if (AuthService.isLoggedIn()) {
$ionicHistory.nextViewOptions({disableBack: true});
$state.go('app.home');
}
....
my states
....
$urlRouterProvider.otherwise('/public/login');
It works perfectly in browser, but when I use app on Android than I see login page for about an 500ms and then it is redirected to the home page.
I have exact same issue.
Could someone point out where nad how to change this, so if user is logged in application already starts at dashboard, if not it starts at login page.
This would help a lot!
You shoud do it in special function in .run() something like this: function ($rootScope, $state, AuthService ) { $rootScope.$on('$stateChangeStart', function (event,next, nextParams, fromState) { if (!AuthService.isAuthenticated()) { if (next.name !== 'login') { event.preventDefault(); $state.go('login'); } } }); }
Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: AngularJS
at ionic.bundle.js:13248
at Scope.$digest (ionic.bundle.js:28901)
at Scope.$apply (ionic.bundle.js:29131)
at bootstrapApply (ionic.bundle.js:14813)
at Object.invoke (ionic.bundle.js:17630)
at doBootstrap (ionic.bundle.js:14811)
at bootstrap (ionic.bundle.js:14831)
at angularInit (ionic.bundle.js:14725)
at HTMLDocument. (ionic.bundle.js:41539)
at fire (jquery-1.12.0.js:3232)
What I’d like to achieve:
At start my application must check if user is logged in (using $localStorage) - done
If user is starting app/webpage and is opening simple url (without # in it) and is logged in it should go to ‘app.playlists’
If user is opening webpage with specific url (# in it) and is logged in it should go to that view
If user is not logged in and is trying to go do anny other view than login it should disallow that and redirect to login page.
Sorry for maybe simple question but I’m building my first application and I’d like to create it as it should be done.
Looks like it something wrong with your logic probably AuthService.isLoggedIn(). Remove event.preventDefault();$state.go(); in both conditions and check is it triggered properly and other data. Also it bad to have 2 if in this situation