Fix error message on token expiration
authorChocobozzz <me@florianbigard.com>
Mon, 7 May 2018 16:40:51 +0000 (18:40 +0200)
committerChocobozzz <me@florianbigard.com>
Mon, 7 May 2018 16:41:10 +0000 (18:41 +0200)
client/src/app/core/auth/auth.service.ts
client/src/app/shared/rest/rest-extractor.service.ts

index 69ae3e5e15174a744cb1292b746ef9f4744c0d47..6223cde6d1501ddd17803b7b9b9a839f98a3a37f 100644 (file)
@@ -39,6 +39,7 @@ export class AuthService {
   private clientSecret: string
   private loginChanged: Subject<AuthStatus>
   private user: AuthUser = null
+  private refreshingTokenObservable: Observable<any>
 
   constructor (
     private http: HttpClient,
@@ -144,6 +145,8 @@ export class AuthService {
   }
 
   refreshAccessToken () {
+    if (this.refreshingTokenObservable) return this.refreshingTokenObservable
+
     console.log('Refreshing token...')
 
     const refreshToken = this.getRefreshToken()
@@ -157,18 +160,23 @@ export class AuthService {
 
     const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
 
-    return this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers })
-                    .map(res => this.handleRefreshToken(res))
-                    .catch(err => {
-                      console.error(err)
-                      console.log('Cannot refresh token -> logout...')
-                      this.logout()
-                      this.router.navigate(['/login'])
-
-                      return Observable.throw({
-                        error: 'You need to reconnect.'
-                      })
-                    })
+    this.refreshingTokenObservable = this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers })
+               .map(res => this.handleRefreshToken(res))
+               .do(() => this.refreshingTokenObservable = null)
+               .catch(err => {
+                 this.refreshingTokenObservable = null
+
+                 console.error(err)
+                 console.log('Cannot refresh token -> logout...')
+                 this.logout()
+                 this.router.navigate([ '/login' ])
+
+                 return Observable.throw({
+                   error: 'You need to reconnect.'
+                 })
+               })
+
+    return this.refreshingTokenObservable
   }
 
   refreshUserInformation () {
index b1e22a76cb04ab62f004122e869a969368cf3eb8..8ed24c727f2c9ed393cb31cc817ed0af0b07e05b 100644 (file)
@@ -33,13 +33,15 @@ export class RestExtractor {
     return target
   }
 
-  handleError (err: HttpErrorResponse) {
+  handleError (err: any) {
     let errorMessage
 
     if (err.error instanceof Error) {
       // A client-side or network error occurred. Handle it accordingly.
       errorMessage = err.error.message
       console.error('An error occurred:', errorMessage)
+    } else if (typeof err.error === 'string') {
+      errorMessage = err.error
     } else if (err.status !== undefined) {
       // A server-side error occurred.
       if (err.error && err.error.errors) {