Client: handle the case when the refreshing token step fails
authorChocobozzz <florian.bigard@gmail.com>
Sat, 1 Oct 2016 07:20:42 +0000 (09:20 +0200)
committerChocobozzz <florian.bigard@gmail.com>
Sat, 1 Oct 2016 07:20:42 +0000 (09:20 +0200)
client/src/app/admin/requests/request-stats/request-stats.component.html
client/src/app/shared/auth/auth-http.service.ts
client/src/app/shared/auth/auth.service.ts

index 04e5937f8fd236ba0ecac8edbd071ca46598a1e5..1b90100e764e20a5301557559329b4a5c884d15f 100644 (file)
@@ -12,7 +12,7 @@
   </div>
 
   <div>
-    <span class="label-description">Total requests:</span>
+    <span class="label-description">Remaining requests:</span>
     {{ stats.requests.length }}
   </div>
 </div>
index 55bb501e6bbed89a2d8c1dc7da512cd1bf005ff3..2392898cae43521a56bcb348af5ea84d5c09ec09 100644 (file)
@@ -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) {
index 2273048c88026c2527c9195fd1f19c2d096d0b6c..e12da0b340ed9fb5331eb849184e1c7387772944 100644 (file)
@@ -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<AuthStatus>;
   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.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) {