Ionic2: check for login, listening for page changes

I’m trying to add a login check in my app using Ionic2 (v2.0.0-alpha.20). In Ionic1 I listened for the locationchange event and redirected somewhere else if you weren’t logged in. Something like this (simplified):

run(['$rootScope', '$location', function($rootScope, $location) {
  $rootScope.$on('$locationChangeStart', function(event, next, current) {
    if(!loggedIn) {

How could I do something similar in Ionic2?

I first tried implementing the onPageWillEnter callback for a page and then setting the root to the LoginPage, but that only worked seemed to work sometimes, usually only the first time you opened that page. Not very reliable.

I tried using the Angular2 Router and using the subscribe function to listen for page changed, but couldn’t get the callback to register.

Could anyone give me a hint how to listen for page changes and acting on them? Or even a better way to check for login?

1 Like

I have a similar question about how to listen for page changes. If there were a central “master page” that had access to some of these universal events and the ability to delegate them, that’d be great. Unfortunately I haven’t been able to determine how to inject the NavController and the ViewController into my @App class which is where I suspect this should happen.

To solve this issue I leveraged a custom event. You can check out the Ionic API docs and read up on the event services - you’ll need to import it of course from ionic-framework/ionic, but then it worked for me.

Now I’ve got an authentication event the fires and I can subscribe to it anywhere.