简体   繁体   English

如何设置应用程序始终登录,直到用户单击ionic3上的注销按钮

[英]How to set the app always login until user click the logout button on ionic3

I created a new mobile app in ionic3 我在ionic3中创建了一个新的移动应用程序

If an user closes the app without clicking the logout button, the user should be logged in if he or she re-opens the app. 如果用户在未单击注销按钮的情况下关闭了该应用程序,则该用户应在重新打开该应用程序后登录。 However, if an user closes the app and re-opens it, login page appears. 但是,如果用户关闭应用程序并重新打开它,则会显示登录页面。

How do I set up the app so the users stay logged in until they click the logout button? 如何设置应用程序,以便用户单击“注销”按钮之前保持登录状态?

Well, the way I think they do that, is tracking the ip. 好吧,我认为他们这样做的方式是跟踪ip。 If the user enters from the same ip, and user has not log out (you have to save that info in a tb) the the script logs him/her in with user info ( that user info also in the former tb along with the ip) 如果用户从同一ip输入,并且用户尚未注销(您必须将该信息保存在tb中),则脚本将使用用户信息(该用户信息也与ip一起在前一个tb中)登录)

YOu should have guessed that 你应该猜到

In my case, I use JWT ( https://jwt.io/ ) for authentication. 就我而言,我使用JWT( https://jwt.io/ )进行身份验证。

So, when users login, the jwt info would be stored in localStorage as well. 因此,当用户登录时,jwt信息也将存储在localStorage中。 Of course, when user logout, JWT info in localStorage will be deleted. 当然,当用户注销时,localStorage中的JWT信息将被删除。

When application is launched, I check localStorage and if there is a valid JWT info (JWT has valid time info as well), I use the JWT info. 启动应用程序时,我检查localStorage,并且如果有有效的JWT信息(JWT也具有有效的时间信息),则使用JWT信息。

I hope it would be helpful :) 我希望这会有所帮助:)

You can recover it by using localstorage 您可以使用localstorage恢复它

I will provide an example for you.As usal app.component.ts is load first so i need to work on it for which page are showing. 我将为您提供一个示例。由于通常app.component.ts加载app.component.ts所以我需要对其进行显示。

import { Component, ViewChild } from '@angular/core';
import { Platform, Nav  } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import {ProfilePage} from "../pages/profile/profile";
import {LoginPage} from "../pages/login/login";

@Component({
  templateUrl: 'app.html'
})

export class MyApp {

  @ViewChild(Nav) nav: Nav;
  rootPage: any;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {

    platform.ready().then(() => {
      this.initializeApp(statusBar, splashScreen);
    });

  }

  initializeApp(statusBar: StatusBar,
                splashScreen: SplashScreen) {

    let username = window.localStorage.getItem('username') ? window.localStorage.getItem('username') : '';
    let password = window.localStorage.getItem('password') ? window.localStorage.getItem('password') : '';

    if(username == 'user' && password == 'user') {
      this.nav.setRoot(ProfilePage);
    }else{
      this.nav.setRoot(LoginPage);
    }
    statusBar.styleDefault();
    splashScreen.hide();
  }
}

Here username and pasword are taken from localstorage if they have no value then its load LoginPage .In LoginPage you need to setup for storing username and password in localstorage. 如果usernamepasword没有值,则从本地存储中获取usernamepassword ,然后加载负载LoginPage 。在LoginPage您需要设置将usernamepassword存储在localstorage中。

  signIn(){

    if (this.loginForm.value.username=="user" && this.loginForm.value.password=="user") {
          window.localStorage.setItem('username', this.loginForm.value.username);
        window.localStorage.setItem('password', this.loginForm.value.password);
          this.navCtrl.setRoot(ProfilePage);
    }
  }

And also when you logout then destroy username and password localstorage value. 而且,当您注销时,销毁usernamepassword localstorage值。

 window.localForage.removeItem('username')
 window.localForage.removeItem('password')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM