frontend: Refactor AuthService to rename methods and add new ones to obtain info about current authenticated user

This commit is contained in:
juancarmore 2025-03-21 01:39:46 +01:00
parent 081722d63c
commit 411388ba53

View File

@ -3,53 +3,74 @@ import { HttpService } from '../http/http.service';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { from, Observable } from 'rxjs'; import { from, Observable } from 'rxjs';
import { HttpErrorResponse } from '@angular/common/http'; import { HttpErrorResponse } from '@angular/common/http';
import { Role, User } from '@lib/typings/ce';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class AuthService { export class AuthService {
protected isAuthenticated = false;
protected hasCheckAuth = false; protected hasCheckAuth = false;
protected isAuthenticated = false;
protected user: User | null = null;
constructor( constructor(
protected httpService: HttpService, protected httpService: HttpService,
protected router: Router protected router: Router
) {} ) {}
async adminLogin(username: string, password: string) { async login(username: string, password: string) {
try { try {
await this.httpService.adminLogin({ username, password }); await this.httpService.login({ username, password });
this.isAuthenticated = true; await this.getAuthenticatedUser();
} catch (error) { } catch (err) {
console.error((error as HttpErrorResponse).error.message); const error = err as HttpErrorResponse;
console.error(error.error.message || error.error);
throw error; throw error;
} }
} }
adminRefresh(): Observable<{ message: string }> { refreshToken(): Observable<{ message: string }> {
return from(this.httpService.adminRefresh()); return from(this.httpService.refreshToken());
} }
async adminLogout() { async logout(redirectTo?: string) {
try { try {
await this.httpService.adminLogout(); await this.httpService.logout();
this.isAuthenticated = false; this.isAuthenticated = false;
this.router.navigate(['console/login']); this.user = null;
if (redirectTo) {
this.router.navigate([redirectTo]);
}
} catch (error) { } catch (error) {
console.error((error as HttpErrorResponse).error.message); console.error((error as HttpErrorResponse).error.message);
} }
} }
async isAdminAuthenticated(): Promise<boolean> { async isUserAuthenticated(): Promise<boolean> {
if (!this.hasCheckAuth) { if (!this.hasCheckAuth) {
try { await this.getAuthenticatedUser();
await this.httpService.adminVerify();
this.isAuthenticated = true;
} catch (error) {
this.isAuthenticated = false;
}
this.hasCheckAuth = true; this.hasCheckAuth = true;
} }
return this.isAuthenticated; return this.isAuthenticated;
} }
getUsername(): string {
return this.user?.username || '';
}
isAdmin(): boolean {
return this.user?.role === Role.ADMIN;
}
private async getAuthenticatedUser() {
try {
const user = await this.httpService.getProfile();
this.user = user;
this.isAuthenticated = true;
} catch (error) {
this.user = null;
this.isAuthenticated = false;
}
}
} }