mirror of
https://github.com/fabioformosa/quartz-manager.git
synced 2026-05-14 22:00:30 +09:00
#6 refactoring completed
This commit is contained in:
@@ -22,8 +22,14 @@ export class ApiService {
|
||||
'Content-Type': 'application/json'
|
||||
});
|
||||
|
||||
private jwtToken: string;
|
||||
|
||||
constructor( private http: HttpClient) { }
|
||||
|
||||
setToken(token: string) {
|
||||
this.jwtToken = token;
|
||||
}
|
||||
|
||||
get(path: string, args?: any): Observable<any> {
|
||||
const options = {
|
||||
headers: this.headers,
|
||||
@@ -56,6 +62,9 @@ export class ApiService {
|
||||
withCredentials: true
|
||||
});
|
||||
|
||||
if(this.jwtToken)
|
||||
req.headers.append('Authorization', `Bearer ${this.jwtToken}`);
|
||||
|
||||
return this.http.request(req)
|
||||
.pipe(
|
||||
filter(response => response instanceof HttpResponse),
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { HttpHeaders } from '@angular/common/http';
|
||||
import { HttpHeaders, HttpResponse } from '@angular/common/http';
|
||||
import { ApiService } from './api.service';
|
||||
import { UserService } from './user.service';
|
||||
import { ConfigService } from './config.service';
|
||||
import { Observable } from 'rxjs';
|
||||
import { map } from 'rxjs/operators';
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
@@ -15,16 +15,33 @@ 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',
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
});
|
||||
const body = `username=${user.username}&password=${user.password}`;
|
||||
return this.apiService.post(this.config.login_url, body, loginHeaders).pipe(map(() => {
|
||||
console.log("Login success");
|
||||
this.userService.getMyInfo().subscribe();
|
||||
}));
|
||||
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();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
signup(user){
|
||||
@@ -48,4 +65,5 @@ export class AuthService {
|
||||
return this.apiService.post(this.config.change_password_url, passwordChanger);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -82,7 +82,6 @@ export class LoginComponent implements OnInit, OnDestroy {
|
||||
this.authService.login(this.form.value)
|
||||
.pipe(delay(1000))
|
||||
.subscribe(data => {
|
||||
this.userService.getMyInfo().subscribe();
|
||||
this.router.navigate([this.returnUrl]);
|
||||
},
|
||||
error => {
|
||||
|
||||
Reference in New Issue
Block a user