Reputation: 184
I want to redirect page to home page (override login page) when it already has token on localStorage. How to do it? I have following code on constructor() at app.component.ts, but it display login first before request completed
statusBar.backgroundColorByHexString('#D32F2F');
splashScreen.hide();
if(localStorage.getItem('token')){
authProvider.silent_login().subscribe(res => {
console.log(res);
if(res.error==0){
this.rootPage = HomePage;
}
})
}
Upvotes: 2
Views: 1356
Reputation: 4782
Can you like
@ViewChild(Nav) nav: Nav;
rootPage: any = null; // Initialize it as null
pages: Array<{title: string, component: any}>;
constructor(public platform: Platform,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public commonProvider: CommonProvider) {
this.commonProvider.retrieve("is_login").then(loggedIn => {
// Assign the right page after checking the status
this.rootPage = loggedIn ? TabsPage : SigninPage;
});
}
Upvotes: 5
Reputation: 184
The solution is generate one page named splash and set root page of app to it and at constructor, we check the token. If fails set root page to login and if success set root page to homepage.
I think thats the only solution. Splash page as credentials checker.
Maybe it helps other
Upvotes: 0