Hello. I have problem with integrating JWT token plugin from Auth0 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 {}
My 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);
}
}
And at least my 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);
}
);
}
}
My 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?
Btw. Sorry for my terrible english