I’m developing Ionic application with Spring framework back-end. Application is simple, check specified URI to check user is authenticated and valid user and then return token. So application using that token to get some JSON from Basic Authentication required URI.
The problem is angular/http is not sending “Authorization” header.
httpclient.ts
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Storage } from '@ionic/storage';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
@Injectable()
export class HttpClient {
constructor(private http: Http, private storage: Storage) {
}
buildHeaders(){
let headers = new Headers({
'Content-Type': 'application/json',
'withCredentials': 'true'
});
return this.storage.get("user").then((user) => {
if(user){
let info = JSON.parse(user);
console.log(info.token);
headers.append('Authorization', 'Basic ' + info.token);
return headers;
}
}
)
}
get(url) {
return Observable
.fromPromise(this.buildHeaders())
.switchMap((headers) => this.http.get(url, { headers: headers }));
}
}
used like this
import { Component } from '@angular/core';
import { AuthProvider } from "../../providers/auth/auth";
import { Storage } from "@ionic/storage";
import { SERVER_URL } from "../../config";
import { HttpClient } from "../../providers/httpclient/httpclient";
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
user: string;
message: string;
list: any;
posts: any;
constructor(private authProvider: AuthProvider,
private storage: Storage,
private http: HttpClient) {
});
}
ionViewWillEnter() {
this.storage.get('user').then(user => {
this.http
.get(`${SERVER_URL}/content/find.json`).subscribe(
response => this.message = response.json(),
err => alert(err)
);
});
}
logout() {
this.authProvider.logout();
}
}
How can i send request with Authorization header?
Ive tried many ways but no help.
ionic info :
global packages:
@ionic/cli-utils : 1.1.2
Cordova CLI : 7.0.1
Ionic CLI : 3.1.2
local packages:
@ionic/app-scripts : 1.3.7
@ionic/cli-plugin-cordova : 1.1.2
@ionic/cli-plugin-ionic-angular : 1.1.2
Ionic Framework : ionic-angular 3.2.1
System:
Node : v7.9.0
OS : Linux 4.4
Xcode : not installed
ios-deploy : not installed
ios-sim : not installed
package.json deps :
"dependencies": {
"@angular/common": "4.1.0",
"@angular/compiler": "4.1.0",
"@angular/compiler-cli": "4.1.0",
"@angular/core": "4.1.0",
"@angular/forms": "4.1.0",
"@angular/http": "4.1.0",
"@angular/platform-browser": "4.1.0",
"@angular/platform-browser-dynamic": "4.1.0",
"@ionic-native/core": "3.7.0",
"@ionic-native/splash-screen": "3.7.0",
"@ionic-native/status-bar": "3.7.0",
"@ionic/storage": "2.0.0",
"cordova-android": "^6.2.3",
"cordova-plugin-console": "^1.0.5",
"cordova-plugin-device": "^1.1.4",
"cordova-plugin-splashscreen": "^4.0.3",
"cordova-plugin-statusbar": "^2.2.2",
"cordova-plugin-whitelist": "^1.3.1",
"cordova-sqlite-storage": "^2.0.4",
"ionic-angular": "3.2.1",
"ionic-plugin-keyboard": "^2.2.1",
"ionicons": "3.0.0",
"ng2-validation": "^4.2.0",
"rxjs": "5.1.1",
"sw-toolbox": "3.6.0",
"zone.js": "0.8.10"
},
"devDependencies": {
"@ionic/app-scripts": "1.3.7",
"@ionic/cli-plugin-cordova": "1.1.2",
"@ionic/cli-plugin-ionic-angular": "1.1.2",
"typescript": "2.2.1"
},