存储将不再设置我的id_token
它一直在工作,我试图从API中插入一个ImageUrl,过了一会儿就停止工作了。我没有遇到任何错误,并且已经尝试了一切。我的console.log在this.storage.get('id_token'(中。然后(token => {...}(;不做任何事情。
在"概述"文件中,我进行相同的检查(从存储中获取'id_token'(以输入(ionviewCanenter(。
它仅在我的Android设备上的Ionic Dev App上起作用(按下页面(,但是.APK和浏览器版本似乎以某种方式破坏了。
以下是所需的代码块
loginservice.ts
import { Http } from '@angular/http';
import { Storage } from '@ionic/storage';
import { Injectable } from '@angular/core';
import { App } from 'ionic-angular';
import 'rxjs/add/operator/map';
@Injectable()
export class LoginServiceProvider {
apiUrl = 'https://api.example.be';
constructor(public http: Http, public storage: Storage, public appCtrl: App) {
}
login(body) {
return new Promise((resolve, reject) => {
this.http.post(this.apiUrl+'/get_token', body)
.subscribe(data => {
let token = data.json();
this.storage.set('id_token', token);
this.storage.get('id_token').then(token => {
console.log('got token');
if(token) {
console.log('token exists');
} else {
console.log('token does not exist');
}
});
})
});
}
}
login.ts
import { LoginServiceProvider } from './../../providers/login-service/login-service';
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { RegisterPage } from '../register/register';
import { OverviewPage } from '../overview/overview';
import { Storage } from '@ionic/storage';
import { LoadingController } from 'ionic-angular';
@Component({
selector: 'page-login',
templateUrl: 'login.html'
})
export class LoginPage {
userData;
apiUrl = 'https://api.example.be';
constructor(public navCtrl: NavController, private loginService: LoginServiceProvider, public storage: Storage, public loadingCtrl: LoadingController) {
}
login() {
this.loginService.login({email: "email@example.com", password: "examplepass"});
return this.storage.get('id_token').then(token => {
this.loadingCtrl.create({
content: 'Logging in...',
duration: 1000,
dismissOnPageChange: true
}).present();
setTimeout(() => {
// Does not work anymore
this.navCtrl.push(OverviewPage).catch(err => {
console.log('not allowed!');
});
}, 1000);
});
}
goToRegister() {
this.navCtrl.push(RegisterPage);
}
forgotPassword() {
}
}
login.html(重要的是(
<button ion-button full color="orange" (click)="login()">Login</button>
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { ErrorHandler, NgModule } from '@angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '@ionic-native/splash-screen';
import { StatusBar } from '@ionic-native/status-bar';
import { MyApp } from './app.component';
import { OverviewPage } from '../pages/overview/overview';
import { LoginPage } from '../pages/login/login';
import { ProfilePage } from '../pages/profile/profile';
import { ProfilePageModule } from '../pages/profile/profile.module';
import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { Http } from '@angular/http';
import { HttpModule } from '@angular/http';
import { HttpClientModule } from '@angular/common/http';
import { Storage, IonicStorageModule } from '@ionic/storage';
import { LoginServiceProvider } from '../providers/login-service/login-service';
export function getAuthHttp(http, storage) {
return new AuthHttp(new AuthConfig({
noJwtError: true,
globalHeaders: [{'Accept': 'application/json'}],
tokenGetter: (() => storage.get('id_token')),
}), http);
}
@NgModule({
declarations: [
MyApp,
LoginPage,
OverviewPage
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp),
HttpModule,
HttpClientModule,
IonicStorageModule.forRoot(),
ProfilePageModule
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
OverviewPage,
LoginPage,
ProfilePage
],
providers: [
StatusBar,
SplashScreen,
{provide: ErrorHandler, useClass: IonicErrorHandler},
{
provide: AuthHttp,
useFactory: getAuthHttp,
deps: [Http, Storage]
},
LoginServiceProvider
]
})
export class AppModule {}
[已解决]
可能是存储的令牌的问题,一切都在不调整1行代码的情况下再次工作。令牌可能已过期,存储器必须设置一个新的,但我很高兴它再次起作用。