Storage won't set my id_token anymore
It was working the whole time, I tried to insert an imageurl from the api and after a while it just stopped working. I get no errors what so ever and have tried everything. My console.log in the this.storage.get('id_token').then(token => { ... }); doesn't do a thing.
In the overview.ts file, I do the same check (getting 'id_token' from storage) to enter (ionViewCanEnter).
It only works (pushing the page) on the ionic dev app on my android device, but the .apk and browser version seem to be broken somehow.
Below are the needed code-blocks
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 (what matters)
<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 {}