Client: handle the case when the refreshing token step fails

pull/10/merge
Chocobozzz 2016-10-01 09:20:42 +02:00
parent e5e756e2d5
commit 14ad0c276b
3 changed files with 30 additions and 10 deletions

View File

@ -12,7 +12,7 @@
</div> </div>
<div> <div>
<span class="label-description">Total requests:</span> <span class="label-description">Remaining requests:</span>
{{ stats.requests.length }} {{ stats.requests.length }}
</div> </div>
</div> </div>

View File

@ -28,7 +28,7 @@ export class AuthHttp extends Http {
return super.request(url, options) return super.request(url, options)
.catch((err) => { .catch((err) => {
if (err.status === 401) { if (err.status === 401) {
return this.handleTokenExpired(err, url, options); return this.handleTokenExpired(url, options);
} }
return Observable.throw(err); return Observable.throw(err);
@ -65,12 +65,13 @@ export class AuthHttp extends Http {
return this.request(url, options); return this.request(url, options);
} }
private handleTokenExpired(err: Response, url: string | Request, options: RequestOptionsArgs) { private handleTokenExpired(url: string | Request, options: RequestOptionsArgs) {
return this.authService.refreshAccessToken().flatMap(() => { return this.authService.refreshAccessToken()
this.setAuthorizationHeader(options.headers); .flatMap(() => {
this.setAuthorizationHeader(options.headers);
return super.request(url, options); return super.request(url, options);
}); });
} }
private setAuthorizationHeader(headers: Headers) { private setAuthorizationHeader(headers: Headers) {

View File

@ -1,5 +1,6 @@
import { Injectable } from '@angular/core'; 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 { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject'; import { Subject } from 'rxjs/Subject';
@ -20,7 +21,11 @@ export class AuthService {
private loginChanged: Subject<AuthStatus>; private loginChanged: Subject<AuthStatus>;
private user: AuthUser = null; 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<AuthStatus>(); this.loginChanged = new Subject<AuthStatus>();
this.loginChangedSource = this.loginChanged.asObservable(); this.loginChangedSource = this.loginChanged.asObservable();
@ -142,7 +147,21 @@ export class AuthService {
return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options)
.map(this.restExtractor.extractDataGet) .map(this.restExtractor.extractDataGet)
.map(res => this.handleRefreshToken(res)) .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) { private fetchUserInformations (obj: any) {