diff options
Diffstat (limited to 'client/src/app/shared/auth/auth-http.service.ts')
-rw-r--r-- | client/src/app/shared/auth/auth-http.service.ts | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/client/src/app/shared/auth/auth-http.service.ts b/client/src/app/shared/auth/auth-http.service.ts new file mode 100644 index 000000000..ff8099a46 --- /dev/null +++ b/client/src/app/shared/auth/auth-http.service.ts | |||
@@ -0,0 +1,77 @@ | |||
1 | import { Injectable } from '@angular/core'; | ||
2 | import { | ||
3 | ConnectionBackend, | ||
4 | Headers, | ||
5 | Http, | ||
6 | Request, | ||
7 | RequestMethod, | ||
8 | RequestOptions, | ||
9 | RequestOptionsArgs, | ||
10 | Response | ||
11 | } from '@angular/http'; | ||
12 | import { Observable } from 'rxjs/Observable'; | ||
13 | |||
14 | import { AuthService } from './auth.service'; | ||
15 | |||
16 | @Injectable() | ||
17 | export class AuthHttp extends Http { | ||
18 | constructor(backend: ConnectionBackend, defaultOptions: RequestOptions, private authService: AuthService) { | ||
19 | super(backend, defaultOptions); | ||
20 | } | ||
21 | |||
22 | request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> { | ||
23 | if (!options) options = {}; | ||
24 | |||
25 | options.headers = new Headers(); | ||
26 | this.setAuthorizationHeader(options.headers); | ||
27 | |||
28 | return super.request(url, options) | ||
29 | .catch((err) => { | ||
30 | if (err.status === 401) { | ||
31 | return this.handleTokenExpired(err, url, options); | ||
32 | } | ||
33 | |||
34 | return Observable.throw(err); | ||
35 | }); | ||
36 | } | ||
37 | |||
38 | delete(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
39 | if (!options) options = {}; | ||
40 | options.method = RequestMethod.Delete; | ||
41 | |||
42 | return this.request(url, options); | ||
43 | } | ||
44 | |||
45 | get(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
46 | if (!options) options = {}; | ||
47 | options.method = RequestMethod.Get; | ||
48 | |||
49 | return this.request(url, options); | ||
50 | } | ||
51 | |||
52 | post(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
53 | if (!options) options = {}; | ||
54 | options.method = RequestMethod.Post; | ||
55 | |||
56 | return this.request(url, options); | ||
57 | } | ||
58 | |||
59 | put(url: string, options?: RequestOptionsArgs): Observable<Response> { | ||
60 | if (!options) options = {}; | ||
61 | options.method = RequestMethod.Put; | ||
62 | |||
63 | return this.request(url, options); | ||
64 | } | ||
65 | |||
66 | private handleTokenExpired(err: Response, url: string | Request, options: RequestOptionsArgs) { | ||
67 | return this.authService.refreshAccessToken().flatMap(() => { | ||
68 | this.setAuthorizationHeader(options.headers); | ||
69 | |||
70 | return super.request(url, options); | ||
71 | }); | ||
72 | } | ||
73 | |||
74 | private setAuthorizationHeader(headers: Headers) { | ||
75 | headers.set('Authorization', `${this.authService.getTokenType()} ${this.authService.getToken()}`); | ||
76 | } | ||
77 | } | ||