2.2 KiB
2.2 KiB
Implementation Plan - Email Services
1. Infrastructure
- Add MailHog to
docker-compose.ymlfor local SMTP testing. - Install
nodemailerand@types/nodemailer.
2. Database Schema
- Update
shared/schema.ts:- Add
password_reset_tokenstable.id(uuid, pk)userId(fk users.id)token(string, unique)expiresAt(timestamp)isUsed(boolean)
- Add validation schemas (
insertPasswordResetTokenSchema).
- Add
3. Email Service (server/email.ts)
- Create
EmailServiceclass. - 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.
- Keys:
- Use
4. Backend Routes (server/routes.ts)
- Modify Registration:
- After successful user creation, call
emailService.sendWelcomeEmail.
- After successful user creation, call
- New Routes:
POST /api/auth/forgot-password:- Input:
email. - Logic: Find user, generate token (uuid), save to DB, send email.
- Input:
POST /api/auth/reset-password:- Input:
token,newPassword. - Logic: Validate token (exists, not used, not expired), hash new password, update user, mark token used.
- Input:
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
AdminUserManagementorSettingspage (optional but requested "add email settings"). - Form to set SMTP host/port/etc.
- Add "Email Settings" section in
7. Configuration
- Add
scripts/test-email-flow.tsto simulate the flow and verify via MailHog API (http://localhost:8025/api/v2/messages).