From 47f8de28346a7aa28425265f316dc26bf2376c45 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 7 May 2018 18:40:51 +0200 Subject: Fix error message on token expiration --- client/src/app/core/auth/auth.service.ts | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'client/src/app/core/auth/auth.service.ts') diff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.ts index 69ae3e5e1..6223cde6d 100644 --- a/client/src/app/core/auth/auth.service.ts +++ b/client/src/app/core/auth/auth.service.ts @@ -39,6 +39,7 @@ export class AuthService { private clientSecret: string private loginChanged: Subject private user: AuthUser = null + private refreshingTokenObservable: Observable 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(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(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 () { -- cgit v1.2.3