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

查看:27
本文介绍了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天全站免登陆