From bd5c83a8cb46eb6da2b25df3b1f6a2a5795d1869 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 20 Jul 2016 16:24:18 +0200 Subject: Client: Add authHttp service that authentificates the http request and optionally refresh the access token if needed --- client/src/app/videos/shared/video.service.ts | 10 +++++----- client/src/app/videos/video-add/video-add.component.ts | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 7 deletions(-) (limited to 'client/src/app/videos') diff --git a/client/src/app/videos/shared/video.service.ts b/client/src/app/videos/shared/video.service.ts index dcbef7717..b4396f767 100644 --- a/client/src/app/videos/shared/video.service.ts +++ b/client/src/app/videos/shared/video.service.ts @@ -5,7 +5,7 @@ import { Observable } from 'rxjs/Observable'; import { Pagination } from './pagination.model'; import { Search } from '../../shared'; import { SortField } from './sort-field.type'; -import { AuthService } from '../../shared'; +import { AuthHttp, AuthService } from '../../shared'; import { Video } from './video.model'; @Injectable() @@ -14,6 +14,7 @@ export class VideoService { constructor( private authService: AuthService, + private authHttp: AuthHttp, private http: Http ) {} @@ -35,10 +36,9 @@ export class VideoService { } removeVideo(id: string) { - const options = this.authService.getAuthRequestOptions(); - return this.http.delete(VideoService.BASE_VIDEO_URL + id, options) - .map(res => res.status) - .catch(this.handleError); + return this.authHttp.delete(VideoService.BASE_VIDEO_URL + id) + .map(res => res.status) + .catch(this.handleError); } searchVideos(search: Search, pagination: Pagination, sort: SortField) { diff --git a/client/src/app/videos/video-add/video-add.component.ts b/client/src/app/videos/video-add/video-add.component.ts index 342935e36..c0f8cb9c4 100644 --- a/client/src/app/videos/video-add/video-add.component.ts +++ b/client/src/app/videos/video-add/video-add.component.ts @@ -130,8 +130,22 @@ export class VideoAddComponent implements OnInit { }; item.onError = (response: string, status: number) => { - this.error = (status === 400) ? response : 'Unknow error'; - console.error(this.error); + // We need to handle manually these cases beceause we use the FileUpload component + if (status === 400) { + this.error = response; + } else if (status === 401) { + this.error = 'Access token was expired, refreshing token...'; + this.authService.refreshAccessToken().subscribe( + () => { + // Update the uploader request header + this.uploader.authToken = this.authService.getRequestHeaderValue(); + this.error += ' access token refreshed. Please retry your request.'; + } + ); + } else { + this.error = 'Unknow error'; + console.error(this.error); + } }; -- cgit v1.2.3