Authentication is tricky for me because at any point, due to third parties involved, the server can decline my request and I’d have to get a new token and authenticate again (don’t ask, it’s not my server). Preferably I would have done this check using Angular 2 CanActivate route guard, but seeing how Ionic 2 doesn’t quite support this yet, I’m thinking of just subscribing to the NavController viewWillEnter event. Here’s what I have so far.
import { Service } from './services/oauth';
@Component({
templateUrl: 'build/app.html'
})
export class App {
@ViewChild(Nav) nav: Nav;
rootPage: any = InboxPage;
constructor(public platform: Platform) {
this.init();
}
init() {
this.platform.ready().then(() => {
StatusBar.styleDefault();
this.nav.viewWillEnter.subscribe(data => {
console.log('viewWillEnter', data);
if (!Service.isAuthenticated && data.name !== "LoginPage") {
debugger; //now what? How do you cancel? return false?
}
});
});
}
}