forked from angular/angular.io
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAuthenticatedHttpService.ts
More file actions
32 lines (28 loc) · 1015 Bytes
/
AuthenticatedHttpService.ts
File metadata and controls
32 lines (28 loc) · 1015 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Injectable, Inject } from '@angular/core';
import { Http, Headers, Response } from '@angular/http';
import { AzureADAuthService } from './authenticators/AzureADAuthService';
import { Observable, Subscriber } from 'rxjs';
@Injectable()
export class AuthenticatedHttpService {
private _authenticator: AzureADAuthService;
private _http: Http;
constructor( @Inject(Http) http: Http, @Inject(AzureADAuthService) authenticator: AzureADAuthService) {
this._authenticator = authenticator;
this._http = http;
}
createAuthorizationHeader(headers: Headers) {
headers.append('Authorization', 'Bearer ' + this._authenticator.getAccessToken());
}
get(url: string) {
let headers = new Headers();
this.createAuthorizationHeader(headers);
return this._http.get(url, { headers: headers });
}
post(url: string, data: any) {
let headers = new Headers();
this.createAuthorizationHeader(headers);
return this._http.post(url, data, {
headers: headers,
});
}
}