diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-10-01 09:20:42 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-10-01 09:20:42 +0200 |
commit | 14ad0c276b4948476d58c82270f2107c8ae5ecd8 (patch) | |
tree | 2a4e1228c8ca26706e8671720eaa5e178f662bd0 /client/src/app/shared/auth/auth.service.ts | |
parent | e5e756e2d5424505db23b26c55b6c9e369146d1b (diff) | |
download | PeerTube-14ad0c276b4948476d58c82270f2107c8ae5ecd8.tar.gz PeerTube-14ad0c276b4948476d58c82270f2107c8ae5ecd8.tar.zst PeerTube-14ad0c276b4948476d58c82270f2107c8ae5ecd8.zip |
Client: handle the case when the refreshing token step fails
Diffstat (limited to 'client/src/app/shared/auth/auth.service.ts')
-rw-r--r-- | client/src/app/shared/auth/auth.service.ts | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/client/src/app/shared/auth/auth.service.ts b/client/src/app/shared/auth/auth.service.ts index 2273048c8..e12da0b34 100644 --- a/client/src/app/shared/auth/auth.service.ts +++ b/client/src/app/shared/auth/auth.service.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import { Injectable } from '@angular/core'; | 1 | import { Injectable } from '@angular/core'; |
2 | import { Headers, Http, URLSearchParams } from '@angular/http'; | 2 | import { Headers, Http, Response, URLSearchParams } from '@angular/http'; |
3 | import { Router } from '@angular/router'; | ||
3 | import { Observable } from 'rxjs/Observable'; | 4 | import { Observable } from 'rxjs/Observable'; |
4 | import { Subject } from 'rxjs/Subject'; | 5 | import { Subject } from 'rxjs/Subject'; |
5 | 6 | ||
@@ -20,7 +21,11 @@ export class AuthService { | |||
20 | private loginChanged: Subject<AuthStatus>; | 21 | private loginChanged: Subject<AuthStatus>; |
21 | private user: AuthUser = null; | 22 | private user: AuthUser = null; |
22 | 23 | ||
23 | constructor(private http: Http, private restExtractor: RestExtractor) { | 24 | constructor( |
25 | private http: Http, | ||
26 | private restExtractor: RestExtractor, | ||
27 | private router: Router | ||
28 | ) { | ||
24 | this.loginChanged = new Subject<AuthStatus>(); | 29 | this.loginChanged = new Subject<AuthStatus>(); |
25 | this.loginChangedSource = this.loginChanged.asObservable(); | 30 | this.loginChangedSource = this.loginChanged.asObservable(); |
26 | 31 | ||
@@ -142,7 +147,21 @@ export class AuthService { | |||
142 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) | 147 | return this.http.post(AuthService.BASE_TOKEN_URL, body.toString(), options) |
143 | .map(this.restExtractor.extractDataGet) | 148 | .map(this.restExtractor.extractDataGet) |
144 | .map(res => this.handleRefreshToken(res)) | 149 | .map(res => this.handleRefreshToken(res)) |
145 | .catch((res) => this.restExtractor.handleError(res)); | 150 | .catch((res: Response) => { |
151 | // The refresh token is invalid? | ||
152 | if (res.status === 400 && res.json() && res.json().error === 'invalid_grant') { | ||
153 | console.error('Cannot refresh token -> logout...'); | ||
154 | this.logout(); | ||
155 | this.router.navigate(['/login']); | ||
156 | |||
157 | return Observable.throw({ | ||
158 | json: '', | ||
159 | text: 'You need to reconnect.' | ||
160 | }); | ||
161 | } | ||
162 | |||
163 | return this.restExtractor.handleError(res); | ||
164 | }); | ||
146 | } | 165 | } |
147 | 166 | ||
148 | private fetchUserInformations (obj: any) { | 167 | private fetchUserInformations (obj: any) { |