feat(core-auth): add AuthService for logout functionality

Add AuthService wrapping OAuthService logout with proper logging.
Refactor RoleService to use private class fields (#) convention.
This commit is contained in:
Lorenz Hilpert
2025-12-03 21:17:15 +01:00
parent 3ed3d0b466
commit 0c546802fa
3 changed files with 31 additions and 16 deletions

View File

@@ -1,10 +1,11 @@
/**
* Core Auth Library
*
* Provides role-based authorization utilities for the ISA Frontend application.
*/
export { RoleService } from './lib/role.service';
export { IfRoleDirective } from './lib/if-role.directive';
export { TokenProvider, TOKEN_PROVIDER, parseJwt } from './lib/token-provider';
export { Role } from './lib/role';
/**
* Core Auth Library
*
* Provides role-based authorization utilities for the ISA Frontend application.
*/
export { AuthService } from './lib/auth.service';
export { RoleService } from './lib/role.service';
export { IfRoleDirective } from './lib/if-role.directive';
export { TokenProvider, TOKEN_PROVIDER, parseJwt } from './lib/token-provider';
export { Role } from './lib/role';

View File

@@ -0,0 +1,14 @@
import { inject, Injectable } from '@angular/core';
import { OAuthService } from 'angular-oauth2-oidc';
import { logger } from '@isa/core/logging';
@Injectable({ providedIn: 'root' })
export class AuthService {
#logger = logger({ service: 'AuthService' });
#oAuthService = inject(OAuthService);
logout(): void {
this.#logger.info('User logging out');
this.#oAuthService.logOut();
}
}

View File

@@ -21,8 +21,8 @@ import { Role } from './role';
providedIn: 'root',
})
export class RoleService {
private readonly _log = logger({ service: 'RoleService' });
private readonly _tokenProvider = inject(TOKEN_PROVIDER);
#logger = logger({ service: 'RoleService' });
#tokenProvider = inject(TOKEN_PROVIDER);
/**
* Check if the authenticated user has specific role(s)
@@ -45,10 +45,10 @@ export class RoleService {
const roles = coerceArray(role);
try {
const userRoles = this._tokenProvider.getClaimByKey('role');
const userRoles = this.#tokenProvider.getClaimByKey('role');
if (!userRoles) {
this._log.debug('No roles found in token claims');
this.#logger.debug('No roles found in token claims');
return false;
}
@@ -57,14 +57,14 @@ export class RoleService {
const hasAllRoles = roles.every((r) => userRolesArray.includes(r));
this._log.debug(`Role check: ${roles.join(', ')} => ${hasAllRoles}`, () => ({
this.#logger.debug(`Role check: ${roles.join(', ')} => ${hasAllRoles}`, () => ({
requiredRoles: roles,
userRoles: userRolesArray,
}));
return hasAllRoles;
} catch (error) {
this._log.error('Error checking roles', error as Error, () => ({ requiredRoles: roles }));
this.#logger.error('Error checking roles', error as Error, () => ({ requiredRoles: roles }));
return false;
}
}