refactor to adapters
This commit is contained in:
14
core/identity/application/use-cases/GetUserUseCase.ts
Normal file
14
core/identity/application/use-cases/GetUserUseCase.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { User } from '../../domain/entities/User';
|
||||
import { IUserRepository } from '../../domain/repositories/IUserRepository';
|
||||
|
||||
export class GetUserUseCase {
|
||||
constructor(private userRepo: IUserRepository) {}
|
||||
|
||||
async execute(userId: string): Promise<User> {
|
||||
const stored = await this.userRepo.findById(userId);
|
||||
if (!stored) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
return User.fromStored(stored);
|
||||
}
|
||||
}
|
||||
29
core/identity/application/use-cases/LoginUseCase.ts
Normal file
29
core/identity/application/use-cases/LoginUseCase.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
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';
|
||||
|
||||
/**
|
||||
* Application Use Case: LoginUseCase
|
||||
*
|
||||
* Handles user login by verifying credentials.
|
||||
*/
|
||||
export class LoginUseCase {
|
||||
constructor(
|
||||
private authRepo: IAuthRepository,
|
||||
private passwordService: IPasswordHashingService
|
||||
) {}
|
||||
|
||||
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');
|
||||
}
|
||||
const isValid = await this.passwordService.verify(password, user.getPasswordHash()!.value);
|
||||
if (!isValid) {
|
||||
throw new Error('Invalid credentials');
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
41
core/identity/application/use-cases/SignupUseCase.ts
Normal file
41
core/identity/application/use-cases/SignupUseCase.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { EmailAddress } from '../../domain/value-objects/EmailAddress';
|
||||
import { UserId } from '../../domain/value-objects/UserId';
|
||||
import { User } from '../../domain/entities/User';
|
||||
import { IAuthRepository } from '../../domain/repositories/IAuthRepository';
|
||||
import { IPasswordHashingService } from '../../domain/services/PasswordHashingService';
|
||||
|
||||
/**
|
||||
* Application Use Case: SignupUseCase
|
||||
*
|
||||
* Handles user registration.
|
||||
*/
|
||||
export class SignupUseCase {
|
||||
constructor(
|
||||
private authRepo: IAuthRepository,
|
||||
private passwordService: IPasswordHashingService
|
||||
) {}
|
||||
|
||||
async execute(email: string, password: string, displayName: string): Promise<User> {
|
||||
const emailVO = EmailAddress.create(email);
|
||||
|
||||
// Check if user already exists
|
||||
const existingUser = await this.authRepo.findByEmail(emailVO);
|
||||
if (existingUser) {
|
||||
throw new Error('User already exists');
|
||||
}
|
||||
|
||||
const hashedPassword = await this.passwordService.hash(password);
|
||||
const passwordHash = await import('../../domain/value-objects/PasswordHash').then(m => m.PasswordHash.fromHash(hashedPassword));
|
||||
|
||||
const userId = UserId.create();
|
||||
const user = User.create({
|
||||
id: userId,
|
||||
displayName,
|
||||
email: emailVO.value,
|
||||
passwordHash,
|
||||
});
|
||||
|
||||
await this.authRepo.save(user);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user