parent
a44b196861
commit
2e24c6fd5d
@ -0,0 +1,55 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
||||||
|
import { BehaviorSubject, Observable, tap } from 'rxjs';
|
||||||
|
import { environment } from '../../environment';
|
||||||
|
import { LocalStorageService } from './localstorage.service';
|
||||||
|
interface LoginResponse {
|
||||||
|
access_token: string;
|
||||||
|
token_type: string;
|
||||||
|
user_id: string;
|
||||||
|
is_admin: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class AuthService {
|
||||||
|
private isAdminSubject = new BehaviorSubject<boolean>(false);
|
||||||
|
isAdmin$ = this.isAdminSubject.asObservable();
|
||||||
|
|
||||||
|
constructor(private http: HttpClient, private localStorageService: LocalStorageService) {
|
||||||
|
const token = this.localStorageService.getToken();
|
||||||
|
if (token) {
|
||||||
|
this.isAdminSubject.next(this.localStorageService.getIsAdmin() === 'true');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
login(username: string, password: string): Observable<LoginResponse> {
|
||||||
|
const payload = new HttpParams()
|
||||||
|
.set('username', username)
|
||||||
|
.set('password', password);
|
||||||
|
|
||||||
|
return this.http.post<LoginResponse>(`${environment.apiURL}/login`, payload).pipe(
|
||||||
|
tap(response => {
|
||||||
|
this.localStorageService.setToken(response.access_token);
|
||||||
|
this.localStorageService.setIsAdmin(response.is_admin);
|
||||||
|
this.isAdminSubject.next(response.is_admin);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
logout(): void {
|
||||||
|
this.localStorageService.removeToken();
|
||||||
|
this.localStorageService.removeIsAdmin();
|
||||||
|
this.isAdminSubject.next(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
isAdmin(): boolean {
|
||||||
|
return this.isAdminSubject.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
getAuthHeaders(): HttpHeaders {
|
||||||
|
const token = this.localStorageService.getToken();
|
||||||
|
return new HttpHeaders().set('Authorization', `Bearer ${token}`);
|
||||||
|
}
|
||||||
|
}
|
@ -1,21 +0,0 @@
|
|||||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
|
||||||
import { Injectable } from '@angular/core';
|
|
||||||
import { Observable } from 'rxjs';
|
|
||||||
import { environment } from '../../environments/environment';
|
|
||||||
|
|
||||||
@Injectable({
|
|
||||||
providedIn: 'root',
|
|
||||||
})
|
|
||||||
export class LoginService {
|
|
||||||
private apiUrl = environment.apiURL;
|
|
||||||
|
|
||||||
constructor(private http: HttpClient) {}
|
|
||||||
|
|
||||||
login(username: string, password: string): Observable<any> {
|
|
||||||
const payload = new HttpParams()
|
|
||||||
.set('username', username)
|
|
||||||
.set('password', password);
|
|
||||||
|
|
||||||
return this.http.post(this.apiUrl + '/login', payload);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in new issue