diff options
Diffstat (limited to 'client/src/app/shared')
-rw-r--r-- | client/src/app/shared/auth/auth-http.service.ts | 13 | ||||
-rw-r--r-- | client/src/app/shared/auth/auth.service.ts | 25 |
2 files changed, 29 insertions, 9 deletions
diff --git a/client/src/app/shared/auth/auth-http.service.ts b/client/src/app/shared/auth/auth-http.service.ts index 55bb501e6..2392898ca 100644 --- a/client/src/app/shared/auth/auth-http.service.ts +++ b/client/src/app/shared/auth/auth-http.service.ts | |||
@@ -28,7 +28,7 @@ export class AuthHttp extends Http { | |||
28 | return super.request(url, options) | 28 | return super.request(url, options) |
29 | .catch((err) => { | 29 | .catch((err) => { |
30 | if (err.status === 401) { | 30 | if (err.status === 401) { |
31 | return this.handleTokenExpired(err, url, options); | 31 | return this.handleTokenExpired(url, options); |
32 | } | 32 | } |
33 | 33 | ||
34 | return Observable.throw(err); | 34 | return Observable.throw(err); |
@@ -65,12 +65,13 @@ export class AuthHttp extends Http { | |||
65 | return this.request(url, options); | 65 | return this.request(url, options); |
66 | } | 66 | } |
67 | 67 | ||
68 | private handleTokenExpired(err: Response, url: string | Request, options: RequestOptionsArgs) { | 68 | private handleTokenExpired(url: string | Request, options: RequestOptionsArgs) { |
69 | return this.authService.refreshAccessToken().flatMap(() => { | 69 | return this.authService.refreshAccessToken() |
70 | this.setAuthorizationHeader(options.headers); | 70 | .flatMap(() => { |
71 | this.setAuthorizationHeader(options.headers); | ||
71 | 72 | ||
72 | return super.request(url, options); | 73 | return super.request(url, options); |
73 | }); | 74 | }); |
74 | } | 75 | } |
75 | 76 | ||
76 | private setAuthorizationHeader(headers: Headers) { | 77 | private setAuthorizationHeader(headers: Headers) { |
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts index 2273048c8..e12da0b34 100644 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Headers, Http, URLSearchParams } from '@angular/http'; | 2 | import { Headers, Http, Response, URLSearchParams } from '@angular/http'; |
3 | import { Router } from '@angular/router'; | ||
3 | import { Observable } from 'rxjs/Observable'; | 4 | import { Observable } from 'rxjs/Observable'; |
4 | import { Subject } from 'rxjs/Subject'; | 5 | import { Subject } from 'rxjs/Subject'; |
5 | 6 | ||
@@ -20,7 +21,11 @@ export class AuthService { | |||
20 | private loginChanged: Subject<AuthStatus>; | 21 | private loginChanged: Subject<AuthStatus>; |
21 | private user: AuthUser = null; | 22 | private user: AuthUser = null; |
22 | 23 | ||
23 | constructor(private http: Http, private restExtractor: RestExtractor) { | 24 | constructor( |
25 | private http: Http, | ||
26 | private restExtractor: RestExtractor, | ||
27 | private router: Router | ||
28 | ) { | ||
24 | this.loginChanged = new Subject<AuthStatus>(); | 29 | this.loginChanged = new Subject<AuthStatus>(); |
25 | this.loginChangedSource = this.loginChanged.asObservable(); | 30 | this.loginChangedSource = this.loginChanged.asObservable(); |
26 | 31 | ||
@@ -142,7 +147,21 @@ export class AuthService { | |||
142 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) | 147 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) |
143 | .map(this.restExtractor.extractDataGet) | 148 | .map(this.restExtractor.extractDataGet) |
144 | .map(res => this.handleRefreshToken(res)) | 149 | .map(res => this.handleRefreshToken(res)) |
145 | .catch((res) => this.restExtractor.handleError(res)); | 150 | .catch((res: Response) => { |
151 | // The refresh token is invalid? | ||
152 | if (res.status === 400 && res.json() && res.json().error === 'invalid_grant') { | ||
153 | console.error('Cannot refresh token -> logout...'); | ||
154 | this.logout(); | ||
155 | this.router.navigate(['/login']); | ||
156 | |||
157 | return Observable.throw({ | ||
158 | json: '', | ||
159 | text: 'You need to reconnect.' | ||
160 | }); | ||
161 | } | ||
162 | |||
163 | return this.restExtractor.handleError(res); | ||
164 | }); | ||
146 | } | 165 | } |
147 | 166 | ||
148 | private fetchUserInformations (obj: any) { | 167 | private fetchUserInformations (obj: any) { |