Do not show login page if user is already logged in

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.

How to fix this?
Thank you.

1 Like

+1
Same problem here :slight_smile:

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'); } } }); }

I’ve added this to my run function

$rootScope.$on('$stateChangeStart', function (event, next, nextParams, fromState) {
    console.log('from',fromState);
    console.log('to',next);

        if (!AuthService.isLoggedIn()) {
            if (next.name !== 'login') {
                console.log('must login!');
                event.preventDefault();
                $state.go('login');
            }
        }
        
        if (AuthService.isLoggedIn()) {
            if (next.name == 'login') {
                console.log('goto dashboard');
                event.preventDefault();
                $state.go('app.playlists');
            }
        }
});

when I open my app at http://localhost:8100/#/login everything works as expected, but when I open http://localhost:8100 (without #) I get this error:

Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: []
http://errors.angularjs.org/1.4.3/$rootScope/infdig?p0=10&p1=[]
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:

  1. At start my application must check if user is logged in (using $localStorage) - done
  2. If user is starting app/webpage and is opening simple url (without # in it) and is logged in it should go to ‘app.playlists’
  3. If user is opening webpage with specific url (# in it) and is logged in it should go to that view
  4. 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

I thought that AuthService might be the problem but I’ve changed function to this:

$rootScope.$on('$stateChangeStart', function (event, next, nextParams, fromState) {
    console.log('from', fromState);
    console.log('to', next);
    var isLoggedIn = true;//AuthService.isLoggedIn();
    console.log('is logged in', isLoggedIn);
    
    if (!isLoggedIn) {
        if (next.name !== 'login') {
            console.log('must login!');
            event.preventDefault();
            $state.go('login');
        }
    } else {
        if (next.name == 'login') {
            console.log('goto dashboard');
            event.preventDefault();
            $state.go('app.playlists');
        }
    }
});

but I get exactly same error.

I’ve searched a bit and I found issue on github: https://github.com/angular-ui/ui-router/issues/1699#issuecomment-144310960

adding this inside config helped:

        //default - throws error
        //$urlRouterProvider.otherwise('/login');
        //this helped!
        $urlRouterProvider.otherwise(function ($injector) {
            var $state = $injector.get("$state");
            $state.go("login");
        });

I’m not sure if this is the best way of doing this, but it works for me.

Commencts and hints from advanced users are welcome :smile: