Files
EnvelopeGenerator/envelope-generator-ui/src/app/services/auth.service.ts
TekH eda30472b9 No changes detected in diff
No code was added or removed in the provided diff; only context lines were present.
2026-02-02 10:14:15 +01:00

49 lines
1.3 KiB
TypeScript

import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable, firstValueFrom, tap } from 'rxjs';
import { API_URL } from '../tokens/index';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private url: string;
constructor(private http: HttpClient) {
const api_url = inject(API_URL);
this.url = `${api_url}/auth`;
}
login(credentials: { username: string; password: string }): Observable<any> {
return this.http.post(`${this.url}/login`, credentials).pipe(
tap({
next: res => this.#IsAuthenticated = true,
error: () => this.#IsAuthenticated = false
})
)
}
logout(): Observable<any> {
return this.http.post(`${this.url}/logout`, {}).pipe(
tap({
next: res => this.#IsAuthenticated = false,
error: () => this.#IsAuthenticated = true
})
);
}
async logoutAsync(): Promise<void> {
return await firstValueFrom(this.logout());
}
isAuthenticated(): Observable<boolean> {
return this.http.get<boolean>(`${this.url}/check`).pipe(
tap(isAuthenticated => this.#IsAuthenticated = isAuthenticated)
);
}
#IsAuthenticated = false;
get IsAuthenticated(): boolean {
return this.#IsAuthenticated;
}
}