diff --git a/quartz-manager-frontend/src/app/services/api.service.ts b/quartz-manager-frontend/src/app/services/api.service.ts index 201e901..4c4ae29 100644 --- a/quartz-manager-frontend/src/app/services/api.service.ts +++ b/quartz-manager-frontend/src/app/services/api.service.ts @@ -1,7 +1,7 @@ import { HttpClient, HttpHeaders, HttpResponse, HttpRequest, HttpEventType, HttpParams } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; -import { catchError, map, filter } from 'rxjs/operators' +import { catchError, map, filter, tap } from 'rxjs/operators' import { serialize } from 'app/shared/utilities/serialize'; export enum RequestMethod { @@ -17,6 +17,17 @@ export enum RequestMethod { @Injectable() export class ApiService { + private static extractTokenFromHttpResponse(res: HttpResponse): string { + let authorization: string = null; + let headers: HttpHeaders = res.headers; + if (headers && headers.has('Authorization')){ + authorization = headers.get('Authorization'); + if(authorization.startsWith('Bearer ')) + authorization = authorization.substring(7); + } + return authorization; + } + headers = new HttpHeaders({ 'Accept': 'application/json', 'Content-Type': 'application/json' @@ -36,9 +47,11 @@ export class ApiService { withCredentials: true }; - if (args) { + if (args) options['params'] = serialize(args); - } + + if(this.jwtToken) + options.headers = options.headers.set('Authorization', `Bearer ${this.jwtToken}`); return this.http.get(path, options) .pipe(catchError(this.checkError.bind(this))); @@ -68,6 +81,10 @@ export class ApiService { return this.http.request(req) .pipe( filter(response => response instanceof HttpResponse), + tap((resp: HttpResponse) => { + let jwtToken = ApiService.extractTokenFromHttpResponse(resp); + this.setToken(jwtToken); + }), map((response: HttpResponse) => response.body), catchError(error => this.checkError(error)) ) @@ -83,4 +100,6 @@ export class ApiService { throw error; } + + } diff --git a/quartz-manager-frontend/src/app/services/auth.service.ts b/quartz-manager-frontend/src/app/services/auth.service.ts index e79decf..c39a395 100644 --- a/quartz-manager-frontend/src/app/services/auth.service.ts +++ b/quartz-manager-frontend/src/app/services/auth.service.ts @@ -15,16 +15,6 @@ export class AuthService { private config: ConfigService, ) { } - private static extractTokenFromHttpResponse(res: HttpResponse): string { - let authorization: string = null; - let headers: HttpHeaders = res.headers; - if (headers.has('Authorization')) - authorization = headers.get('Authorization'); - if(authorization.startsWith('Bearer ')) - authorization = authorization.substring(7); - return authorization; - } - login(user) { const loginHeaders = new HttpHeaders({ 'Accept': 'application/json', @@ -33,10 +23,6 @@ export class AuthService { const body = `username=${user.username}&password=${user.password}`; return this.apiService.post(this.config.login_url, body, loginHeaders) .pipe( - tap((resp: HttpResponse) => { - let jwtToken = AuthService.extractTokenFromHttpResponse(resp); - this.apiService.setToken(jwtToken); - }), map(() => { console.log("Login success"); this.userService.getMyInfo().subscribe(); @@ -57,6 +43,7 @@ export class AuthService { logout() { return this.apiService.post(this.config.logout_url, {}) .pipe(map(() => { + this.apiService.setToken(null); this.userService.currentUser = null; })); }