From 14ad0c276b4948476d58c82270f2107c8ae5ecd8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sat, 1 Oct 2016 09:20:42 +0200 Subject: Client: handle the case when the refreshing token step fails --- .../request-stats/request-stats.component.html | 2 +- client/src/app/shared/auth/auth-http.service.ts | 13 +++++------ client/src/app/shared/auth/auth.service.ts | 25 +++++++++++++++++++--- 3 files changed, 30 insertions(+), 10 deletions(-) (limited to 'client/src') diff --git a/client/src/app/admin/requests/request-stats/request-stats.component.html b/client/src/app/admin/requests/request-stats/request-stats.component.html index 04e5937f8..1b90100e7 100644 --- a/client/src/app/admin/requests/request-stats/request-stats.component.html +++ b/client/src/app/admin/requests/request-stats/request-stats.component.html @@ -12,7 +12,7 @@
- Total requests: + Remaining requests: {{ stats.requests.length }}
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 { return super.request(url, options) .catch((err) => { if (err.status === 401) { - return this.handleTokenExpired(err, url, options); + return this.handleTokenExpired(url, options); } return Observable.throw(err); @@ -65,12 +65,13 @@ export class AuthHttp extends Http { return this.request(url, options); } - private handleTokenExpired(err: Response, url: string | Request, options: RequestOptionsArgs) { - return this.authService.refreshAccessToken().flatMap(() => { - this.setAuthorizationHeader(options.headers); + private handleTokenExpired(url: string | Request, options: RequestOptionsArgs) { + return this.authService.refreshAccessToken() + .flatMap(() => { + this.setAuthorizationHeader(options.headers); - return super.request(url, options); - }); + return super.request(url, options); + }); } 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 @@ import { Injectable } from '@angular/core'; -import { Headers, Http, URLSearchParams } from '@angular/http'; +import { Headers, Http, Response, URLSearchParams } from '@angular/http'; +import { Router } from '@angular/router'; import { Observable } from 'rxjs/Observable'; import { Subject } from 'rxjs/Subject'; @@ -20,7 +21,11 @@ export class AuthService { private loginChanged: Subject; private user: AuthUser = null; - constructor(private http: Http, private restExtractor: RestExtractor) { + constructor( + private http: Http, + private restExtractor: RestExtractor, + private router: Router + ) { this.loginChanged = new Subject(); this.loginChangedSource = this.loginChanged.asObservable(); @@ -142,7 +147,21 @@ export class AuthService { return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) .map(this.restExtractor.extractDataGet) .map(res => this.handleRefreshToken(res)) - .catch((res) => this.restExtractor.handleError(res)); + .catch((res: Response) => { + // The refresh token is invalid? + if (res.status === 400 && res.json() && res.json().error === 'invalid_grant') { + console.error('Cannot refresh token -> logout...'); + this.logout(); + this.router.navigate(['/login']); + + return Observable.throw({ + json: '', + text: 'You need to reconnect.' + }); + } + + return this.restExtractor.handleError(res); + }); } private fetchUserInformations (obj: any) { -- cgit v1.2.3