* Register a new user * POST /api/auth/register
(request: Request, env: Env, _ctx: ExecutionContext, _routeContext: RouteContext)
| 44 | * POST /api/auth/register |
| 45 | */ |
| 46 | static async register(request: Request, env: Env, _ctx: ExecutionContext, _routeContext: RouteContext): Promise<Response> { |
| 47 | try { |
| 48 | // Check if OAuth providers are configured - if yes, block email/password registration |
| 49 | if (AuthController.hasOAuthProviders(env)) { |
| 50 | return AuthController.createErrorResponse( |
| 51 | 'Email/password registration is not available when OAuth providers are configured. Please use OAuth login instead.', |
| 52 | 403 |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | const bodyResult = await AuthController.parseJsonBody(request); |
| 57 | if (!bodyResult.success) { |
| 58 | return bodyResult.response!; |
| 59 | } |
| 60 | |
| 61 | const validatedData = registerSchema.parse(bodyResult.data); |
| 62 | |
| 63 | if (env.ALLOWED_EMAIL && validatedData.email !== env.ALLOWED_EMAIL) { |
| 64 | return AuthController.createErrorResponse( |
| 65 | 'Email Whitelisting is enabled. Please use the allowed email to register.', |
| 66 | 403 |
| 67 | ); |
| 68 | } |
| 69 | |
| 70 | const authService = new AuthService(env); |
| 71 | const result = await authService.register(validatedData, request); |
| 72 | |
| 73 | const response = AuthController.createSuccessResponse( |
| 74 | formatAuthResponse(result.user, result.sessionId, result.expiresAt) |
| 75 | ); |
| 76 | |
| 77 | setSecureAuthCookies(response, { |
| 78 | accessToken: result.accessToken, |
| 79 | accessTokenExpiry: SessionService.config.sessionTTL |
| 80 | }); |
| 81 | |
| 82 | // Rotate CSRF token on successful registration if configured |
| 83 | if (CsrfService.defaults.rotateOnAuth) { |
| 84 | CsrfService.rotateToken(response); |
| 85 | } |
| 86 | |
| 87 | return response; |
| 88 | } catch (error) { |
| 89 | if (error instanceof SecurityError) { |
| 90 | return AuthController.createErrorResponse(error.message, error.statusCode); |
| 91 | } |
| 92 | |
| 93 | return AuthController.handleError(error, 'register user'); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Login with email and password |
nothing calls this directly
no test coverage detected