Ionic2 - No authorization header send
Hello. I have problem with integrating JWT token plugin with least Ionic2. I have absolutly no error but request is send without authorization header. My code: app.module
import { NgModule } from '@angular/core';
import { IonicApp, IonicModule } from 'ionic-angular';
import { MyApp } from './app.component';
import { AboutPage } from '../pages/about/about';
import { ContactPage } from '../pages/contact/contact';
import { HomePage } from '../pages/home/home';
import { TabsPage } from '../pages/tabs/tabs';
import { LoginPage } from '../pages/login/login';
import { AuthHttp, AuthConfig } from 'angular2-jwt';
import { Storage } from '@ionic/storage';
import { Http } from '@angular/http';
import { AuthService } from '../services/auth.service';
import { NoteService } from '../services/note.service';
let storage: Storage = new Storage();
export function getAuthHttp(http) {
return new AuthHttp(new AuthConfig({
headerPrefix: 'JWT',
noJwtError: true,
globalHeaders: [{'Accept': 'application/json'}],
tokenGetter: (() => storage.get('id_token')),
}), http);
}
@NgModule({
declarations: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
LoginPage
],
imports: [
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
AboutPage,
ContactPage,
HomePage,
TabsPage,
LoginPage
],
providers: [
AuthService,
AuthHttp,
{
provide: AuthHttp,
useFactory: getAuthHttp,
deps: [Http]
},
Storage,
NoteService,
]
})
export class AppModule {}
Service:
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { AuthHttp } from 'angular2-jwt';
import { AppParameters } from '../parameters';
import { AuthFormModel } from '../models/authForm.model';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class NoteService {
private headers = new Headers({ 'Content-Type': 'application/json' });
private note_url: string = AppParameters.ENDPOINT_URL + '/notes/';
private notes: any;
constructor(private authHttp: AuthHttp) {}
getUserNotes(){
return this.authHttp.get(this.note_url);
}
}
Component
import { Component } from '@angular/core';
import { NoteService } from '../../services/note.service';
import { NavController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
notes: any;
constructor(
public navCtrl: NavController,
public noteService: NoteService
) {}
ngOnInit(){
this.noteService.getUserNotes().subscribe(
data => this.notes = data,
err => console.log(err),
() => {
console.log('request complete');
console.log(this.notes);
}
);
}
}
And package.json
{
"name": "ionic-hello-world",
"author": "Ionic Framework",
"homepage": "http://ionicframework.com/",
"private": true,
"scripts": {
"ionic:build": "ionic-app-scripts build",
"ionic:serve": "ionic-app-scripts serve"
},
"dependencies": {
"@angular/common": "2.1.1",
"@angular/compiler": "2.1.1",
"@angular/compiler-cli": "2.1.1",
"@angular/core": "2.1.1",
"@angular/forms": "2.1.1",
"@angular/http": "2.1.1",
"@angular/platform-browser": "2.1.1",
"@angular/platform-browser-dynamic": "2.1.1",
"@angular/platform-server": "2.1.1",
"@ionic/storage": "1.1.6",
"angular2-jwt": "^0.1.25",
"ionic-angular": "2.0.0-rc.3",
"ionic-native": "2.2.3",
"ionicons": "3.0.0",
"rxjs": "5.0.0-beta.12",
"zone.js": "0.6.26"
},
"devDependencies": {
"@ionic/app-scripts": "0.0.45",
"typescript": "2.0.6"
},
"cordovaPlugins": [
"cordova-plugin-whitelist",
"cordova-plugin-console",
"cordova-plugin-statusbar",
"cordova-plugin-device",
"cordova-plugin-splashscreen",
"ionic-plugin-keyboard"
],
"cordovaPlatforms": [],
"description": "noteman_js: An Ionic project"
}
Everything work, absolutly no error in console but header is still missing. Maybe i forget something? I try configuration from JWT plugin and lot of configurations from stack overflow. Btw. Sorry for my terrible english :)
I can't see any issues at first glance. Do you for sure have an id_token in storage? It's possible there just isn't anything saved there to retrieve with storage.get.
Yes, i'm sure id_token is in storage and i can dump it with:
storage.get('id_token').then( (token)=>{ console.log(token); } ) in my component.
Can you try removing your other config keys in your AuthConfig? Just to see if something might be interfering. So test with this:
export function getAuthHttp(http) {
return new AuthHttp(new AuthConfig({
tokenGetter: (() => storage.get('id_token')),
}), http);
}
After removing noJwtError: false, I get empty AuthHttpError(). Maybe becouse storage.get() is async? I have no idea what's wrong :<
angular2-jwt handles the promise that comes from storage.get so that shouldn't be causing any issue. Can you post a minimum reproducible repo and I'll take a look?
@chenkie If you have bitbucket account i could give you access to my project, if not i can post project on github.
Sure, @cienki
I give you access to frontend and backend written in python if you want ;)
@sierran I tried your app and it seems to be working for me if I set a token into storage as id_token manually (I didn't try your server). Perhaps it's something with the way you're setting the token on login?
As a test, try setting the token manually somewhere, perhaps app.module.ts one time.
...
storage.set('id_token', 'ey...')
and then try it again to see if the header gets sent.
I think this is my bad. I think I must manually using helpers chceck if is token expired but now I see this is automatically checked and if token is expired angular2-jwt throw AuthHttpError() without description. Other strange behavior I see is if you wanna get token after save for example:
this.storage.set('id_token', myAwesomeToken);
this.storage.get('id_token').then((token)=>{console.log(token);});
Gives me null on token. So After logIn action i have again AuthHttpError() but if I refresh page after that everything work like charm. I think is now Ionic2 problem. I really appreciate the help from you, when I visit Cannada i gonna buy you a big beer! Thank you and have nice day :)
Ok is Ionic2 Bug. If you see in documentation of ionic2 storage example
`import { Storage } from '@ionic/storage';
export class MyApp {
constructor(storage: Storage) {
storage.set('name', 'Max');
storage.get('name').then((val) => {
console.log('Your name is', val);
})
}
}
is not correct and give you null. But:
`import { Storage } from '@ionic/storage';
export class MyApp {
constructor(storage: Storage) {
storage.set('name', 'Max').then(()=>{
storage.get('name').then((val) => {
console.log('Your name is', val);
})
});
}
}
Works :)
No problem and sounds good, I look forward to the beer :)