Skip to content

Commit

Permalink
refactor: use promises for error handler and remove console.log
Browse files Browse the repository at this point in the history
  • Loading branch information
renanfranca committed Sep 20, 2024
1 parent cdeffdd commit 61da9e4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { AuthenticatedUser } from '@/auth/domain/AuthenticatedUser';
export interface AuthRepository {
currentUser(): Promise<AuthenticatedUser>;
login(): Promise<void>;
logout(): Promise<boolean>;
logout(): Promise<void>;
authenticated(): Promise<boolean>;
refreshToken(): Promise<string>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,23 @@ import { KeycloakHttp } from './KeycloakHttp';
export class KeycloakAuthRepository implements AuthRepository {
constructor(private readonly keycloakHttp: KeycloakHttp) {}

async currentUser(): Promise<AuthenticatedUser> {
try {
return await this.keycloakHttp.currentUser();
} catch (error) {
console.error('Authentication failed', error);
return { isAuthenticated: false, username: '', token: '' };
}
currentUser(): Promise<AuthenticatedUser> {
return this.keycloakHttp.currentUser();
}

async login(): Promise<void> {
try {
await this.keycloakHttp.login();
} catch (error) {
console.error('Login failed', error);
throw error;
}
login(): Promise<void> {
return this.keycloakHttp.login();
}

async logout(): Promise<boolean> {
try {
await this.keycloakHttp.logout();
return true;
} catch (error) {
console.error('Logout failed', error);
return false;
}
logout(): Promise<void> {
return this.keycloakHttp.logout();
}

async authenticated(): Promise<boolean> {
try {
return await this.keycloakHttp.authenticated();
} catch (error) {
console.error('isAuthenticated check failed', error);
return false;
}
authenticated(): Promise<boolean> {
return this.keycloakHttp.authenticated();
}

async refreshToken(): Promise<string> {
try {
return await this.keycloakHttp.refreshToken();
} catch (error) {
console.error('Token refresh failed', error);
return '';
}
refreshToken(): Promise<string> {
return this.keycloakHttp.refreshToken();
}
}

0 comments on commit 61da9e4

Please sign in to comment.