* Register a new user
(data: RegistrationData, request: Request)
| 67 | * Register a new user |
| 68 | */ |
| 69 | async register(data: RegistrationData, request: Request): Promise<AuthResult> { |
| 70 | try { |
| 71 | // Validate email format using centralized utility |
| 72 | const emailValidation = validateEmail(data.email); |
| 73 | if (!emailValidation.valid) { |
| 74 | throw new SecurityError( |
| 75 | SecurityErrorType.INVALID_INPUT, |
| 76 | emailValidation.error || 'Invalid email format', |
| 77 | 400 |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | // Validate password using centralized utility |
| 82 | const passwordValidation = validatePassword(data.password, undefined, { |
| 83 | email: data.email, |
| 84 | name: data.name |
| 85 | }); |
| 86 | if (!passwordValidation.valid) { |
| 87 | throw new SecurityError( |
| 88 | SecurityErrorType.INVALID_INPUT, |
| 89 | passwordValidation.errors!.join(', '), |
| 90 | 400 |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | // Check if user already exists |
| 95 | const existingUser = await this.database |
| 96 | .select() |
| 97 | .from(schema.users) |
| 98 | .where(eq(schema.users.email, data.email.toLowerCase())) |
| 99 | .get(); |
| 100 | |
| 101 | if (existingUser) { |
| 102 | throw new SecurityError( |
| 103 | SecurityErrorType.INVALID_INPUT, |
| 104 | 'Email already registered', |
| 105 | 400 |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | // Hash password |
| 110 | const passwordHash = await this.passwordService.hash(data.password); |
| 111 | |
| 112 | // Create user |
| 113 | const userId = generateId(); |
| 114 | const now = new Date(); |
| 115 | |
| 116 | // Store user as verified immediately (no OTP verification required) |
| 117 | await this.database.insert(schema.users).values({ |
| 118 | id: userId, |
| 119 | email: data.email.toLowerCase(), |
| 120 | passwordHash, |
| 121 | displayName: data.name || data.email.split('@')[0], |
| 122 | emailVerified: true, // Set as verified immediately |
| 123 | provider: 'email', |
| 124 | providerId: userId, |
| 125 | createdAt: now, |
| 126 | updatedAt: now |
no test coverage detected