auth
This commit is contained in:
@@ -17,7 +17,7 @@ export type SignupResult = {
|
||||
user: User;
|
||||
};
|
||||
|
||||
export type SignupErrorCode = 'USER_ALREADY_EXISTS' | 'REPOSITORY_ERROR';
|
||||
export type SignupErrorCode = 'USER_ALREADY_EXISTS' | 'WEAK_PASSWORD' | 'INVALID_DISPLAY_NAME' | 'REPOSITORY_ERROR';
|
||||
|
||||
export type SignupApplicationError = ApplicationErrorCode<SignupErrorCode, { message: string }>;
|
||||
|
||||
@@ -36,8 +36,18 @@ export class SignupUseCase implements UseCase<SignupInput, void, SignupErrorCode
|
||||
|
||||
async execute(input: SignupInput): Promise<Result<void, SignupApplicationError>> {
|
||||
try {
|
||||
// Validate email format
|
||||
const emailVO = EmailAddress.create(input.email);
|
||||
|
||||
// Validate password strength
|
||||
if (!this.isPasswordStrong(input.password)) {
|
||||
return Result.err({
|
||||
code: 'WEAK_PASSWORD',
|
||||
details: { message: 'Password must be at least 8 characters and contain uppercase, lowercase, and number' },
|
||||
});
|
||||
}
|
||||
|
||||
// Check if user exists
|
||||
const existingUser = await this.authRepo.findByEmail(emailVO);
|
||||
if (existingUser) {
|
||||
return Result.err({
|
||||
@@ -46,10 +56,12 @@ export class SignupUseCase implements UseCase<SignupInput, void, SignupErrorCode
|
||||
});
|
||||
}
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await this.passwordService.hash(input.password);
|
||||
const passwordHashModule = await import('../../domain/value-objects/PasswordHash');
|
||||
const passwordHash = passwordHashModule.PasswordHash.fromHash(hashedPassword);
|
||||
|
||||
// Create user (displayName validation happens in User entity constructor)
|
||||
const userId = UserId.create();
|
||||
const user = User.create({
|
||||
id: userId,
|
||||
@@ -63,6 +75,18 @@ export class SignupUseCase implements UseCase<SignupInput, void, SignupErrorCode
|
||||
this.output.present({ user });
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
// Handle specific validation errors from User entity
|
||||
if (error instanceof Error) {
|
||||
if (error.message.includes('Name must be at least') ||
|
||||
error.message.includes('Name can only contain') ||
|
||||
error.message.includes('Please use your real name')) {
|
||||
return Result.err({
|
||||
code: 'INVALID_DISPLAY_NAME',
|
||||
details: { message: error.message },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const message =
|
||||
error instanceof Error && error.message
|
||||
? error.message
|
||||
@@ -78,4 +102,12 @@ export class SignupUseCase implements UseCase<SignupInput, void, SignupErrorCode
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private isPasswordStrong(password: string): boolean {
|
||||
if (password.length < 8) return false;
|
||||
if (!/[a-z]/.test(password)) return false;
|
||||
if (!/[A-Z]/.test(password)) return false;
|
||||
if (!/\d/.test(password)) return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user