refactor use cases
This commit is contained in:
@@ -2,6 +2,22 @@ import { EmailAddress } from '../../domain/value-objects/EmailAddress';
|
||||
import { User } from '../../domain/entities/User';
|
||||
import { IAuthRepository } from '../../domain/repositories/IAuthRepository';
|
||||
import { IPasswordHashingService } from '../../domain/services/PasswordHashingService';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
|
||||
|
||||
export type LoginInput = {
|
||||
email: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
export type LoginResult = {
|
||||
user: User;
|
||||
};
|
||||
|
||||
export type LoginErrorCode = 'INVALID_CREDENTIALS' | 'REPOSITORY_ERROR';
|
||||
|
||||
export type LoginApplicationError = ApplicationErrorCode<LoginErrorCode, { message: string }>;
|
||||
|
||||
/**
|
||||
* Application Use Case: LoginUseCase
|
||||
@@ -10,20 +26,52 @@ import { IPasswordHashingService } from '../../domain/services/PasswordHashingSe
|
||||
*/
|
||||
export class LoginUseCase {
|
||||
constructor(
|
||||
private authRepo: IAuthRepository,
|
||||
private passwordService: IPasswordHashingService
|
||||
private readonly authRepo: IAuthRepository,
|
||||
private readonly passwordService: IPasswordHashingService,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<LoginResult>,
|
||||
) {}
|
||||
|
||||
async execute(email: string, password: string): Promise<User> {
|
||||
const emailVO = EmailAddress.create(email);
|
||||
const user = await this.authRepo.findByEmail(emailVO);
|
||||
if (!user || !user.getPasswordHash()) {
|
||||
throw new Error('Invalid credentials');
|
||||
async execute(input: LoginInput): Promise<Result<void, LoginApplicationError>> {
|
||||
try {
|
||||
const emailVO = EmailAddress.create(input.email);
|
||||
const user = await this.authRepo.findByEmail(emailVO);
|
||||
|
||||
if (!user || !user.getPasswordHash()) {
|
||||
return Result.err({
|
||||
code: 'INVALID_CREDENTIALS',
|
||||
details: { message: 'Invalid credentials' },
|
||||
} as LoginApplicationError);
|
||||
}
|
||||
|
||||
const passwordHash = user.getPasswordHash()!;
|
||||
const isValid = await this.passwordService.verify(input.password, passwordHash.value);
|
||||
|
||||
if (!isValid) {
|
||||
return Result.err({
|
||||
code: 'INVALID_CREDENTIALS',
|
||||
details: { message: 'Invalid credentials' },
|
||||
} as LoginApplicationError);
|
||||
}
|
||||
|
||||
const result: LoginResult = { user };
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
? error.message
|
||||
: 'Failed to execute LoginUseCase';
|
||||
|
||||
this.logger.error('LoginUseCase.execute failed', error instanceof Error ? error : undefined, {
|
||||
input,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
} as LoginApplicationError);
|
||||
}
|
||||
const isValid = await this.passwordService.verify(password, user.getPasswordHash()!.value);
|
||||
if (!isValid) {
|
||||
throw new Error('Invalid credentials');
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user