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) {
$location.path('/login');
}
});
}]);
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?