【发布时间】:2017-05-31 13:28:33
【问题描述】:
我有一个 Ionic 2 应用程序,我想在其中实现注销功能。我想在本地存储中将 Json Web Token 的值设置为 null,然后在设置值后,将用户发送到登录页面。
我遇到了一个问题,即应用程序在将用户带到登录页面之前没有等待设置 JWT 的值。这是一个问题,因为在登录页面上,如果用户有有效的 JWT,我有一个自动登录的功能。因为程序没有阻塞并等待在存储中设置值,所以用户在注销后立即重新登录。
在将用户发送回登录页面之前,如何等待设置令牌的值?
注销功能:
logout() {
this.storage.ready().then(() => {
this.storage.set('token', '').then(data => {
this.navCtrl.setRoot(LoginPage);
});
});
CheckAuthentication 功能:
checkAuthentication() {
return new Promise((resolve, reject) => {
this.storage.get('token').then((value) => {
this.token = value;
let headers = new Headers();
headers.append('Authorization', this.token);
this.http.get('apiURL', { headers: headers })
.subscribe(res => {
resolve(res);
}, (err) => {
reject(err);
});
});
});
}
IonViewWillLoad:
ionViewWillLoad(){
//Check if already authenticated
this.auth.checkAuthentication().then((res) => {
console.log("Already authorized");
this.loading.dismiss();
this.navCtrl.setRoot(HomePage);
}, (err) => {
console.log("Not already authorized");
this.loading.dismiss();
});}
【问题讨论】:
标签: angular typescript promise ionic2 json-web-token