refactor
This commit is contained in:
@@ -4,7 +4,7 @@ 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';
|
||||
import type { UseCaseOutputPort, Logger, UseCase } from '@core/shared/application';
|
||||
|
||||
export type LoginInput = {
|
||||
email: string;
|
||||
@@ -24,40 +24,43 @@ export type LoginApplicationError = ApplicationErrorCode<LoginErrorCode, { messa
|
||||
*
|
||||
* Handles user login by verifying credentials.
|
||||
*/
|
||||
export class LoginUseCase {
|
||||
export class LoginUseCase implements UseCase<LoginInput, LoginResult, LoginErrorCode> {
|
||||
constructor(
|
||||
private readonly authRepo: IAuthRepository,
|
||||
private readonly passwordService: IPasswordHashingService,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<LoginResult>,
|
||||
private readonly output: UseCaseOutputPort<Result<LoginResult, LoginApplicationError>>,
|
||||
) {}
|
||||
|
||||
async execute(input: LoginInput): Promise<Result<void, LoginApplicationError>> {
|
||||
async execute(input: LoginInput): Promise<Result<LoginResult, LoginApplicationError>> {
|
||||
try {
|
||||
const emailVO = EmailAddress.create(input.email);
|
||||
const user = await this.authRepo.findByEmail(emailVO);
|
||||
|
||||
if (!user || !user.getPasswordHash()) {
|
||||
return Result.err({
|
||||
const result = Result.err<LoginResult, LoginApplicationError>({
|
||||
code: 'INVALID_CREDENTIALS',
|
||||
details: { message: 'Invalid credentials' },
|
||||
} as LoginApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const passwordHash = user.getPasswordHash()!;
|
||||
const isValid = await this.passwordService.verify(input.password, passwordHash.value);
|
||||
|
||||
if (!isValid) {
|
||||
return Result.err({
|
||||
const result = Result.err<LoginResult, LoginApplicationError>({
|
||||
code: 'INVALID_CREDENTIALS',
|
||||
details: { message: 'Invalid credentials' },
|
||||
} as LoginApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
const result: LoginResult = { user };
|
||||
const result = Result.ok<LoginResult, LoginApplicationError>({ user });
|
||||
this.output.present(result);
|
||||
|
||||
return Result.ok(undefined);
|
||||
return result;
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
@@ -68,10 +71,12 @@ export class LoginUseCase {
|
||||
input,
|
||||
});
|
||||
|
||||
return Result.err({
|
||||
const result = Result.err<LoginResult, LoginApplicationError>({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: { message },
|
||||
} as LoginApplicationError);
|
||||
});
|
||||
this.output.present(result);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user