Ionic 2 Auth-如果本地存储中有用户,则跳过登录页面 [英] Ionic 2 auth - Skip login page if there's an user on local storage

查看:122
本文介绍了Ionic 2 Auth-如果本地存储中有用户,则跳过登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ionic 2应用程序,可以在Rails 5后端进行身份验证.登录用户后,我会将其信息存储在本地存储中. 因此,一旦用户打开了应用程序并且之前已经登录过,就应该跳过登录页面.我正在尝试在app.component设置根页面上执行此操作,具体取决于是否在本地存储上有关于用户的信息,但是storage.get方法似乎是异步的,因此在我检查之后便会执行,因此一直认为它是错误的.

I have an Ionic 2 application that does an authentication on my Rails 5 backend. Once the user is logged, I store his informations on local storage. So, once the user open the apps and he has already logged before, the login page should be skipped. I'm trying to do that on my app.component setting my root page depending on if there's information about the user on local storage or not, but the storage.get method appears to be asynchronous, so it is executing after my check, so it's always considering it false.

有什么想法我可以解决这个问题吗?

Any ideas how I could fix that?

推荐答案

您可以在从存储中获取值后设置根页面,如下所示:

You can set the root page after getting the value from the storage 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;
        });
      });
    }

}

在这种情况下,如果用户已经登录,则根页面将是HomePage;如果未登录,则是LoginPage.

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

这篇关于Ionic 2 Auth-如果本地存储中有用户,则跳过登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆