From d592e0a9b2931c7c9cbedb27fb8efc9aaacad9bb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Sep 2017 11:57:49 +0200 Subject: Move to HttpClient and PrimeNG data table --- client/src/app/shared/auth/auth-http.service.ts | 93 ---------------------- .../app/shared/auth/auth-interceptor.service.ts | 62 +++++++++++++++ client/src/app/shared/auth/index.ts | 2 +- 3 files changed, 63 insertions(+), 94 deletions(-) delete mode 100644 client/src/app/shared/auth/auth-http.service.ts create mode 100644 client/src/app/shared/auth/auth-interceptor.service.ts (limited to 'client/src/app/shared/auth') diff --git a/client/src/app/shared/auth/auth-http.service.ts b/client/src/app/shared/auth/auth-http.service.ts deleted file mode 100644 index 0fbaab0a8..000000000 --- a/client/src/app/shared/auth/auth-http.service.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { Injectable } from '@angular/core' -import { - ConnectionBackend, - Headers, - Http, - Request, - RequestMethod, - RequestOptions, - RequestOptionsArgs, - Response, - XHRBackend -} from '@angular/http' -import { Observable } from 'rxjs/Observable' - -import { AuthService } from '../../core' - -@Injectable() -export class AuthHttp extends Http { - constructor (backend: ConnectionBackend, defaultOptions: RequestOptions, private authService: AuthService) { - super(backend, defaultOptions) - } - - request (url: string | Request, options?: RequestOptionsArgs): Observable { - if (!options) options = {} - - options.headers = new Headers() - this.setAuthorizationHeader(options.headers) - - return super.request(url, options) - .catch((err) => { - if (err.status === 401) { - return this.handleTokenExpired(url, options) - } - - return Observable.throw(err) - }) - } - - delete (url: string, options?: RequestOptionsArgs): Observable { - if (!options) options = {} - options.method = RequestMethod.Delete - - return this.request(url, options) - } - - get (url: string, options?: RequestOptionsArgs): Observable { - if (!options) options = {} - options.method = RequestMethod.Get - - return this.request(url, options) - } - - post (url: string, body: any, options?: RequestOptionsArgs): Observable { - if (!options) options = {} - options.method = RequestMethod.Post - options.body = body - - return this.request(url, options) - } - - put (url: string, body: any, options?: RequestOptionsArgs): Observable { - if (!options) options = {} - options.method = RequestMethod.Put - options.body = body - - return this.request(url, options) - } - - private handleTokenExpired (url: string | Request, options: RequestOptionsArgs) { - return this.authService.refreshAccessToken() - .flatMap(() => { - this.setAuthorizationHeader(options.headers) - - return super.request(url, options) - }) - } - - private setAuthorizationHeader (headers: Headers) { - headers.set('Authorization', this.authService.getRequestHeaderValue()) - } -} - -export function useFactory (backend: XHRBackend, defaultOptions: RequestOptions, authService: AuthService) { - return new AuthHttp(backend, defaultOptions, authService) -} - -export const AUTH_HTTP_PROVIDERS = [ - { - provide: AuthHttp, - useFactory, - deps: [ XHRBackend, RequestOptions, AuthService ] - } -] diff --git a/client/src/app/shared/auth/auth-interceptor.service.ts b/client/src/app/shared/auth/auth-interceptor.service.ts new file mode 100644 index 000000000..1e890d8f3 --- /dev/null +++ b/client/src/app/shared/auth/auth-interceptor.service.ts @@ -0,0 +1,62 @@ +import { Injectable, Injector } from '@angular/core' +import { + HttpInterceptor, + HttpRequest, + HttpEvent, + HttpHandler, HTTP_INTERCEPTORS +} from '@angular/common/http' +import { Observable } from 'rxjs/Observable' + +import { AuthService } from '../../core' +import 'rxjs/add/operator/switchMap' + +@Injectable() +export class AuthInterceptor implements HttpInterceptor { + private authService: AuthService + + // https://github.com/angular/angular/issues/18224#issuecomment-316957213 + constructor (private injector: Injector) {} + + intercept (req: HttpRequest, next: HttpHandler): Observable> { + if (this.authService === undefined) { + this.authService = this.injector.get(AuthService) + } + + const authReq = this.cloneRequestWithAuth(req) + + // Pass on the cloned request instead of the original request + // Catch 401 errors (refresh token expired) + return next.handle(authReq) + .catch(err => { + if (err.status === 401) { + return this.handleTokenExpired(req, next) + } + + return Observable.throw(err) + }) + } + + private handleTokenExpired (req: HttpRequest, next: HttpHandler): Observable> { + return this.authService.refreshAccessToken() + .switchMap(() => { + const authReq = this.cloneRequestWithAuth(req) + + return next.handle(authReq) + }) + } + + private cloneRequestWithAuth (req: HttpRequest) { + const authHeaderValue = this.authService.getRequestHeaderValue() + + if (authHeaderValue === null) return req + + // Clone the request to add the new header + return req.clone({ headers: req.headers.set('Authorization', authHeaderValue) }) + } +} + +export const AUTH_INTERCEPTOR_PROVIDER = { + provide: HTTP_INTERCEPTORS, + useClass: AuthInterceptor, + multi: true +} diff --git a/client/src/app/shared/auth/index.ts b/client/src/app/shared/auth/index.ts index 0f2bfb0d6..84a07196f 100644 --- a/client/src/app/shared/auth/index.ts +++ b/client/src/app/shared/auth/index.ts @@ -1 +1 @@ -export * from './auth-http.service' +export * from './auth-interceptor.service' -- cgit v1.2.3