[英]How to set the app always login until user click the logout button on ionic3
我在ionic3中創建了一個新的移動應用程序
如果用戶在未單擊注銷按鈕的情況下關閉了該應用程序,則該用戶應在重新打開該應用程序后登錄。 但是,如果用戶關閉應用程序並重新打開它,則會顯示登錄頁面。
如何設置應用程序,以便用戶單擊“注銷”按鈕之前保持登錄狀態?
好吧,我認為他們這樣做的方式是跟蹤ip。 如果用戶從同一ip輸入,並且用戶尚未注銷(您必須將該信息保存在tb中),則腳本將使用用戶信息(該用戶信息也與ip一起在前一個tb中)登錄)
你應該猜到
就我而言,我使用JWT( https://jwt.io/ )進行身份驗證。
因此,當用戶登錄時,jwt信息也將存儲在localStorage中。 當然,當用戶注銷時,localStorage中的JWT信息將被刪除。
啟動應用程序時,我檢查localStorage,並且如果有有效的JWT信息(JWT也具有有效的時間信息),則使用JWT信息。
我希望這會有所幫助:)
您可以使用localstorage
恢復它
我將為您提供一個示例。由於通常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();
}
}
如果username
和pasword
沒有值,則從本地存儲中獲取username
和password
,然后加載負載LoginPage
。在LoginPage
您需要設置將username
和password
存儲在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);
}
}
而且,當您注銷時,銷毀username
和password
localstorage值。
window.localForage.removeItem('username')
window.localForage.removeItem('password')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.