Ionic 2 auth - Skip login page if user is on local storage

I have an Ionic 2 application that authenticates against my Rails 5 server. After the user logs in, I save their local storage information. So, once the user opens the apps and he has already registered previously, the login page should be skipped. I am trying to do this on my app.component

setup of my root page depending on whether there is user information in local storage or not, but the method storage.get

seems to be asynchronous, so it is executed after my check, so this is always considered false.

Any ideas how I could fix this?

+3


source to share


1 answer


You can set the root page after getting the value from the store like this:

@Component({
    templateUrl: 'app.html'
})
export class MyApp {
    @ViewChild(Nav) navCtrl: Nav;

    public rootPage; // Just declare the property, don't set a value here

    constructor(...) {
      this.platform.ready().then(() => {
        Splashscreen.hide();

        // Get the status from the storage
        this.storage.get('login:status').then(loggedIn => {
          this.rootPage = loggedIn ? HomePage : LoginPage;
        });
      });
    }

}

      



In this case, if the user is already registered, the root page will be HomePage, and if he is not logged in, LoginPage.

+12


source







All Articles