Moch. Rizal Rachmadani
Moch. Rizal Rachmadani

Reputation: 184

Ionic 2 redirect to another page after app boot

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

Answers (2)

Paresh Gami
Paresh Gami

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

Moch. Rizal Rachmadani
Moch. Rizal Rachmadani

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

Related Questions