|
| 1 | +import { $inject, t } from "@alepha/core"; |
| 2 | +import { $action } from "@alepha/server"; |
| 3 | +import { $passwordResetEmail } from "../descriptors/$passwordResetEmail.ts"; |
| 4 | +import { SessionService } from "../services/SessionService.ts"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Actions for password reset functionality. |
| 8 | + * |
| 9 | + * This class provides API endpoints for: |
| 10 | + * - Requesting a password reset (sends email with token) |
| 11 | + * - Validating a reset token |
| 12 | + * - Resetting the password with a valid token |
| 13 | + */ |
| 14 | +export class PasswordResetActions { |
| 15 | + protected readonly sessionService = $inject(SessionService); |
| 16 | + protected readonly passwordResetEmail = $passwordResetEmail(); |
| 17 | + |
| 18 | + /** |
| 19 | + * Request a password reset. |
| 20 | + * Generates a reset token and sends an email to the user. |
| 21 | + * |
| 22 | + * POST /api/password-reset/request |
| 23 | + */ |
| 24 | + public requestPasswordReset = $action({ |
| 25 | + schema: { |
| 26 | + body: t.object({ |
| 27 | + email: t.string({ format: "email" }), |
| 28 | + resetUrl: t.string(), |
| 29 | + }), |
| 30 | + response: t.object({ |
| 31 | + success: t.boolean(), |
| 32 | + message: t.string(), |
| 33 | + }), |
| 34 | + }, |
| 35 | + handler: async ({ body }) => { |
| 36 | + const expiresInMinutes = 60; |
| 37 | + const token = await this.sessionService.requestPasswordReset( |
| 38 | + body.email, |
| 39 | + expiresInMinutes, |
| 40 | + ); |
| 41 | + |
| 42 | + // Only send email if token was generated (user exists) |
| 43 | + if (token) { |
| 44 | + // Build the full reset URL with token |
| 45 | + const resetUrlWithToken = `${body.resetUrl}?token=${token}`; |
| 46 | + |
| 47 | + await this.passwordResetEmail.send(body.email, { |
| 48 | + email: body.email, |
| 49 | + resetUrl: resetUrlWithToken, |
| 50 | + expiresInMinutes, |
| 51 | + }); |
| 52 | + } |
| 53 | + |
| 54 | + // Always return success to prevent email enumeration |
| 55 | + return { |
| 56 | + success: true, |
| 57 | + message: |
| 58 | + "If an account exists with this email, a password reset link has been sent.", |
| 59 | + }; |
| 60 | + }, |
| 61 | + }); |
| 62 | + |
| 63 | + /** |
| 64 | + * Validate a password reset token. |
| 65 | + * Checks if the token is valid and not expired. |
| 66 | + * |
| 67 | + * GET /api/password-reset/validate |
| 68 | + */ |
| 69 | + public validateResetToken = $action({ |
| 70 | + schema: { |
| 71 | + query: t.object({ |
| 72 | + token: t.string(), |
| 73 | + }), |
| 74 | + response: t.object({ |
| 75 | + valid: t.boolean(), |
| 76 | + email: t.optional(t.string({ format: "email" })), |
| 77 | + }), |
| 78 | + }, |
| 79 | + handler: async ({ query }) => { |
| 80 | + try { |
| 81 | + const email = await this.sessionService.validateResetToken(query.token); |
| 82 | + return { |
| 83 | + valid: true, |
| 84 | + email, |
| 85 | + }; |
| 86 | + } catch (_error) { |
| 87 | + return { |
| 88 | + valid: false, |
| 89 | + }; |
| 90 | + } |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + /** |
| 95 | + * Reset password with a valid token. |
| 96 | + * Updates the user's password and invalidates all sessions. |
| 97 | + * |
| 98 | + * POST /api/password-reset/reset |
| 99 | + */ |
| 100 | + public resetPassword = $action({ |
| 101 | + schema: { |
| 102 | + body: t.object({ |
| 103 | + token: t.string(), |
| 104 | + newPassword: t.string({ minLength: 8 }), |
| 105 | + }), |
| 106 | + response: t.object({ |
| 107 | + success: t.boolean(), |
| 108 | + message: t.string(), |
| 109 | + }), |
| 110 | + }, |
| 111 | + handler: async ({ body }) => { |
| 112 | + await this.sessionService.resetPassword(body.token, body.newPassword); |
| 113 | + |
| 114 | + return { |
| 115 | + success: true, |
| 116 | + message: "Password has been reset successfully. Please log in.", |
| 117 | + }; |
| 118 | + }, |
| 119 | + }); |
| 120 | +} |
0 commit comments