mirror of
https://github.com/fabioformosa/quartz-manager.git
synced 2025-12-26 20:33:15 +09:00
#6 fixed auth header handling
This commit is contained in:
@@ -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<any>): 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<any>) => {
|
||||
let jwtToken = ApiService.extractTokenFromHttpResponse(resp);
|
||||
this.setToken(jwtToken);
|
||||
}),
|
||||
map((response: HttpResponse<any>) => response.body),
|
||||
catchError(error => this.checkError(error))
|
||||
)
|
||||
@@ -83,4 +100,6 @@ export class ApiService {
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -15,16 +15,6 @@ export class AuthService {
|
||||
private config: ConfigService,
|
||||
) { }
|
||||
|
||||
private static extractTokenFromHttpResponse(res: HttpResponse<any>): 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<any>) => {
|
||||
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;
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user