diff options
author | Chocobozzz <me@florianbigard.com> | 2018-05-07 18:40:51 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-05-07 18:41:10 +0200 |
commit | 47f8de28346a7aa28425265f316dc26bf2376c45 (patch) | |
tree | d249ee979f3488f92a2917e7b3b016db08aca8db | |
parent | fd5e57bbe2accbdb16b6aa65337c5ef44b5bd8fb (diff) | |
download | PeerTube-47f8de28346a7aa28425265f316dc26bf2376c45.tar.gz PeerTube-47f8de28346a7aa28425265f316dc26bf2376c45.tar.zst PeerTube-47f8de28346a7aa28425265f316dc26bf2376c45.zip |
Fix error message on token expiration
-rw-r--r-- | client/src/app/core/auth/auth.service.ts | 32 | ||||
-rw-r--r-- | client/src/app/shared/rest/rest-extractor.service.ts | 4 |
2 files changed, 23 insertions, 13 deletions
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 { | |||
39 | private clientSecret: string | 39 | private clientSecret: string |
40 | private loginChanged: Subject<AuthStatus> | 40 | private loginChanged: Subject<AuthStatus> |
41 | private user: AuthUser = null | 41 | private user: AuthUser = null |
42 | private refreshingTokenObservable: Observable<any> | ||
42 | 43 | ||
43 | constructor ( | 44 | constructor ( |
44 | private http: HttpClient, | 45 | private http: HttpClient, |
@@ -144,6 +145,8 @@ export class AuthService { | |||
144 | } | 145 | } |
145 | 146 | ||
146 | refreshAccessToken () { | 147 | refreshAccessToken () { |
148 | if (this.refreshingTokenObservable) return this.refreshingTokenObservable | ||
149 | |||
147 | console.log('Refreshing token...') | 150 | console.log('Refreshing token...') |
148 | 151 | ||
149 | const refreshToken = this.getRefreshToken() | 152 | const refreshToken = this.getRefreshToken() |
@@ -157,18 +160,23 @@ export class AuthService { | |||
157 | 160 | ||
158 | const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded') | 161 | const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded') |
159 | 162 | ||
160 | return this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers }) | 163 | this.refreshingTokenObservable = this.http.post<UserRefreshToken>(AuthService.BASE_TOKEN_URL, body, { headers }) |
161 | .map(res => this.handleRefreshToken(res)) | 164 | .map(res => this.handleRefreshToken(res)) |
162 | .catch(err => { | 165 | .do(() => this.refreshingTokenObservable = null) |
163 | console.error(err) | 166 | .catch(err => { |
164 | console.log('Cannot refresh token -> logout...') | 167 | this.refreshingTokenObservable = null |
165 | this.logout() | 168 | |
166 | this.router.navigate(['/login']) | 169 | console.error(err) |
167 | 170 | console.log('Cannot refresh token -> logout...') | |
168 | return Observable.throw({ | 171 | this.logout() |
169 | error: 'You need to reconnect.' | 172 | this.router.navigate([ '/login' ]) |
170 | }) | 173 | |
171 | }) | 174 | return Observable.throw({ |
175 | error: 'You need to reconnect.' | ||
176 | }) | ||
177 | }) | ||
178 | |||
179 | return this.refreshingTokenObservable | ||
172 | } | 180 | } |
173 | 181 | ||
174 | refreshUserInformation () { | 182 | refreshUserInformation () { |
diff --git a/client/src/app/shared/rest/rest-extractor.service.ts b/client/src/app/shared/rest/rest-extractor.service.ts index b1e22a76c..8ed24c727 100644 --- a/client/src/app/shared/rest/rest-extractor.service.ts +++ b/client/src/app/shared/rest/rest-extractor.service.ts | |||
@@ -33,13 +33,15 @@ export class RestExtractor { | |||
33 | return target | 33 | return target |
34 | } | 34 | } |
35 | 35 | ||
36 | handleError (err: HttpErrorResponse) { | 36 | handleError (err: any) { |
37 | let errorMessage | 37 | let errorMessage |
38 | 38 | ||
39 | if (err.error instanceof Error) { | 39 | if (err.error instanceof Error) { |
40 | // A client-side or network error occurred. Handle it accordingly. | 40 | // A client-side or network error occurred. Handle it accordingly. |
41 | errorMessage = err.error.message | 41 | errorMessage = err.error.message |
42 | console.error('An error occurred:', errorMessage) | 42 | console.error('An error occurred:', errorMessage) |
43 | } else if (typeof err.error === 'string') { | ||
44 | errorMessage = err.error | ||
43 | } else if (err.status !== undefined) { | 45 | } else if (err.status !== undefined) { |
44 | // A server-side error occurred. | 46 | // A server-side error occurred. |
45 | if (err.error && err.error.errors) { | 47 | if (err.error && err.error.errors) { |