# Implementation Plan - Email Services ## 1. Infrastructure - Add MailHog to `docker-compose.yml` for local SMTP testing. - Install `nodemailer` and `@types/nodemailer`. ## 2. Database Schema - Update `shared/schema.ts`: - Add `password_reset_tokens` table. - `id` (uuid, pk) - `userId` (fk users.id) - `token` (string, unique) - `expiresAt` (timestamp) - `isUsed` (boolean) - Add validation schemas (`insertPasswordResetTokenSchema`). ## 3. Email Service (`server/email.ts`) - Create `EmailService` class. - Methods: - `sendWelcomeEmail(user)` - `sendPasswordResetEmail(user, token)` - Configuration: - Use `nodemailer`. - Fetch SMTP settings from `storage.getSystemSettings` (fallback to env vars or MailHog defaults for dev). - Keys: `smtp_host`, `smtp_port`, `smtp_user`, `smtp_pass`, `smtp_secure`, `smtp_from`. ## 4. Backend Routes (`server/routes.ts`) - **Modify Registration**: - After successful user creation, call `emailService.sendWelcomeEmail`. - **New Routes**: - `POST /api/auth/forgot-password`: - Input: `email`. - Logic: Find user, generate token (uuid), save to DB, send email. - `POST /api/auth/reset-password`: - Input: `token`, `newPassword`. - Logic: Validate token (exists, not used, not expired), hash new password, update user, mark token used. ## 5. Storage (`server/storage.ts`) - Update `IStorage`, `MemStorage`, `DbStorage`: - `createPasswordResetToken(token)` - `getPasswordResetToken(token)` - `markPasswordResetTokenUsed(tokenId)` ## 6. Frontend - **Forgot Password Page** (`/forgot-password`): - Form: Email input. - Action: `POST /api/auth/forgot-password`. - **Reset Password Page** (`/reset-password`): - Form: New Password, Confirm Password. - Params: `?token=...` from URL. - Action: `POST /api/auth/reset-password`. - **Login Page Update**: - Add "Forgot Password?" link. - **Admin Settings**: - Add "Email Settings" section in `AdminUserManagement` or `Settings` page (optional but requested "add email settings"). - Form to set SMTP host/port/etc. ## 7. Configuration - Add `scripts/test-email-flow.ts` to simulate the flow and verify via MailHog API (`http://localhost:8025/api/v2/messages`).