presenter refactoring

This commit is contained in:
2025-12-20 17:06:11 +01:00
parent 92be9d2e1b
commit e9d6f90bb2
109 changed files with 4159 additions and 1283 deletions

View File

@@ -15,6 +15,8 @@ import type { IPasswordHashingService } from '@core/identity/domain/services/Pas
import type { Logger } from "@core/shared/application";
import { AUTH_REPOSITORY_TOKEN, IDENTITY_SESSION_PORT_TOKEN, LOGGER_TOKEN, PASSWORD_HASHING_SERVICE_TOKEN, USER_REPOSITORY_TOKEN } from './AuthProviders';
import { AuthSessionDTO, LoginParams, SignupParams, AuthenticatedUserDTO } from './dtos/AuthDto';
import { AuthSessionPresenter } from './presenters/AuthSessionPresenter';
import { CommandResultPresenter } from './presenters/CommandResultPresenter';
@Injectable()
export class AuthService {
@@ -50,7 +52,7 @@ export class AuthService {
};
}
async getCurrentSession(): Promise<AuthSessionDTO | null> {
async getCurrentSession(): Promise<AuthSessionPresenter | null> {
this.logger.debug('[AuthService] Attempting to get current session.');
const coreSession = await this.identitySessionPort.getCurrentSession();
if (!coreSession) {
@@ -59,36 +61,34 @@ export class AuthService {
const user = await this.userRepository.findById(coreSession.user.id); // Use userRepository to fetch full user
if (!user) {
// If session exists but user doesn't in DB, perhaps clear session?
this.logger.warn(`[AuthService] Session found for user ID ${coreSession.user.id}, but user not found in repository.`);
await this.identitySessionPort.clearSession(); // Clear potentially stale session
return null;
// If session exists but user doesn't in DB, perhaps clear session?
this.logger.warn(`[AuthService] Session found for user ID ${coreSession.user.id}, but user not found in repository.`);
await this.identitySessionPort.clearSession(); // Clear potentially stale session
return null;
}
const authenticatedUserDTO = this.mapUserToAuthenticatedUserDTO(User.fromStored(user));
return {
token: coreSession.token,
user: authenticatedUserDTO,
};
const presenter = new AuthSessionPresenter();
presenter.present({ token: coreSession.token, user: authenticatedUserDTO });
return presenter;
}
async signupWithEmail(params: SignupParams): Promise<AuthSessionDTO> {
async signupWithEmail(params: SignupParams): Promise<AuthSessionPresenter> {
this.logger.debug(`[AuthService] Attempting signup for email: ${params.email}`);
const user = await this.signupUseCase.execute(params.email, params.password, params.displayName);
// Create session after successful signup
const authenticatedUserDTO = this.mapUserToAuthenticatedUserDTO(user);
const coreDto = this.mapToCoreAuthenticatedUserDTO(authenticatedUserDTO);
const session = await this.identitySessionPort.createSession(coreDto);
return {
token: session.token,
user: authenticatedUserDTO,
};
const presenter = new AuthSessionPresenter();
presenter.present({ token: session.token, user: authenticatedUserDTO });
return presenter;
}
async loginWithEmail(params: LoginParams): Promise<AuthSessionDTO> {
async loginWithEmail(params: LoginParams): Promise<AuthSessionPresenter> {
this.logger.debug(`[AuthService] Attempting login for email: ${params.email}`);
try {
const user = await this.loginUseCase.execute(params.email, params.password);
@@ -97,10 +97,9 @@ export class AuthService {
const coreDto = this.mapToCoreAuthenticatedUserDTO(authenticatedUserDTO);
const session = await this.identitySessionPort.createSession(coreDto);
return {
token: session.token,
user: authenticatedUserDTO,
};
const presenter = new AuthSessionPresenter();
presenter.present({ token: session.token, user: authenticatedUserDTO });
return presenter;
} catch (error) {
this.logger.error(`[AuthService] Login failed for email ${params.email}:`, error instanceof Error ? error : new Error(String(error)));
throw new InternalServerErrorException('Login failed due to invalid credentials or server error.');
@@ -108,8 +107,11 @@ export class AuthService {
}
async logout(): Promise<void> {
async logout(): Promise<CommandResultPresenter> {
this.logger.debug('[AuthService] Attempting logout.');
const presenter = new CommandResultPresenter();
await this.logoutUseCase.execute();
presenter.present({ success: true });
return presenter;
}
}