存储不再设置我的 id_token
它一直在工作,我试图从 api 插入一个 imageurl,过了一会儿它就停止了工作。我从来没有遇到任何错误,并且已经尝试了一切。我的 console.log 在 this.storage.get('id_token').then(token => { ... }); 不做任何事情。
在overview.ts 文件中,我执行相同的检查(从存储中获取'id_token')以输入(ionViewCanEnter)。
它只能在我的 android 设备上的 ionic dev 应用程序上工作(推送页面),但 .apk 和浏览器版本似乎以某种方式被破坏。
以下是所需的代码块
登录服务.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');
}
});
})
});
}
}
登录.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 {}