45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import { inject, Injectable } from '@angular/core';
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
import { LoginRequest, PasswordConfirmationRequest, RegisterRequest, User } from './auth.types';
|
|
import { Observable, switchMap } from 'rxjs';
|
|
import { API_URL, BASE_URL } from '../core/tokens/api-urls';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class AuthService {
|
|
private http: HttpClient = inject(HttpClient);
|
|
private baseUrl = inject(BASE_URL);
|
|
private apiUrl = inject(API_URL);
|
|
|
|
public login(credentials: LoginRequest) {
|
|
return this.getSanctumCookie().pipe(
|
|
switchMap(() => this.http.post(`${this.baseUrl}/login`, credentials)),
|
|
);
|
|
}
|
|
|
|
public register(credentials: RegisterRequest) {
|
|
return this.getSanctumCookie().pipe(
|
|
switchMap(() => this.http.post(`${this.baseUrl}/register`, credentials)),
|
|
);
|
|
}
|
|
|
|
public logout() {
|
|
return this.http.post(`${this.baseUrl}/logout`, {});
|
|
}
|
|
|
|
public getCurrentUser(): Observable<User> {
|
|
return this.http.get<User>(`${this.apiUrl}/me`);
|
|
}
|
|
|
|
public confirmPassword(data: PasswordConfirmationRequest): Observable<any> {
|
|
return this.getSanctumCookie().pipe(
|
|
switchMap(() => this.http.post(`${this.baseUrl}/user/confirm-password`, data)),
|
|
);
|
|
}
|
|
|
|
private getSanctumCookie(): Observable<null> {
|
|
return this.http.get<null>(`${this.baseUrl}/sanctum/csrf-cookie`);
|
|
}
|
|
}
|