Skip to content

Email for professionals and teams

Merge remote-tracking branch 'origin/main' into pr/33

Hieu Nguyen (Jack)40d450dcd924
diff --cc CLAUDE.md
index d3fa852,3c63691..65cd782
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@@ -79,10 -79,10 +79,14 @@@ Schema lives in one file: `src/db/schem
The setup path only ever initializes an empty database — it refuses to touch one that already has tables.
+### Password reset and MFA
+
+`reset_email` on `users` is the destination for reset links (`src/lib/auth/password-reset.ts`); links are hashed, single-use, 30 minutes, and redeeming one revokes every session. Reset mail is sent by `sendSystemEmail` (`src/lib/email/system-mail.ts`), which writes straight to the send binding from the first admin mailbox on a sending-enabled domain, so nothing lands in Sent and no webhooks fire. If no domain can send, the request still returns 200 and a warning is logged. TOTP lives in `src/lib/auth/totp.ts` (RFC 6238 over Web Crypto, no dependency); the secret is stored on `users` at enrolment but only counts once `totp_enabled` is set by a verified code. A login with MFA returns `{ mfaRequired, challengeToken }` (`login_challenges`, 5 minutes) instead of a session, and `/api/auth/mfa/verify` finishes it with a TOTP or recovery code. Password changes and admin resets call `deleteUserSessions`.
+
+ ### Search is an FTS5 index kept by triggers
+
+ `messages_fts` (migration 0030) is an external-content FTS5 table over `messages`; three triggers in the same migration keep it in sync on insert, update and delete, so no application code touches the index. `buildSearchConditions` in `src/lib/search/conditions.ts` turns the Gmail-style grammar (`src/lib/search/query-utils.ts`) into a `rowid IN (SELECT rowid FROM messages_fts WHERE messages_fts MATCH ?)` predicate plus plain column filters. Two consequences: the bootstrap SQL in `src/lib/setup/migration.ts` is split with `splitSqlStatements`, which keeps trigger bodies whole; and the backup coverage check skips `messages_fts%`, since the shadow tables are derived and repopulate on restore. `wrangler d1 export` does not work on databases with virtual tables; the app's own JSON backup is unaffected.
+
### Access control
Two independent auth surfaces:
diff --cc docs/api.md
index 13096b3,144162c..8d11337
--- a/docs/api.md
+++ b/docs/api.md
@@@ -57,12 -57,22 +57,28 @@@ Messages composed in Mailflare are sen
The dashboard composer accepts up to 10 attachments, with a 10 MB limit per file and a 20 MB combined limit. Attachment metadata is stored in D1 and file content is stored in R2. Downloads require access to the mailbox containing the message.
+## Password reset and two-factor authentication
+
+`POST /api/auth/password-reset/request` with `{ email }` always answers `200 { ok: true }`; when the account exists and has a recovery email, a single-use link valid for 30 minutes is mailed there. `POST /api/auth/password-reset/confirm` with `{ token, password }` sets the password and signs the account out everywhere.
+
+When two-factor authentication is on, `POST /api/auth/login` returns `{ ok: true, mfaRequired: true, challengeToken }` instead of a session. `POST /api/auth/mfa/verify` with `{ challengeToken, code }` completes the sign-in; `code` is a 6-digit TOTP or one of the recovery codes. Challenges expire after 5 minutes. Enrolment, recovery codes and turning it off are under `/api/settings/mfa/*` (session auth) and always re-check the password.
+
+ ## Searching
+
+ `GET /api/messages?q=...` (session) and `GET /api/v1/messages?q=...` (API key, `read` scope) accept the same query grammar, backed by an FTS5 index over subject, sender, recipients and body:
+
+ | Syntax | Meaning |
+ |---|---|
+ | `invoice` | prefix match anywhere (`inv` finds "invoice") |
+ | `"private window"` | exact phrase |
+ | `-word` | exclude |
+ | `from:maya`, `to:sam`, `subject:report` | restrict a term to one field (`to:` covers Cc) |
+ | `has:attachment` | at least one non-inline attachment |
+ | `is:unread`, `is:read`, `is:starred` | flags |
+ | `after:2026-09-01`, `before:2026-09-30` | date bounds (UTC, `before` exclusive) |
+
+ Terms combine with AND. Admins can check or rebuild the index with `GET` / `POST /api/admin/search-index`; triggers keep it current, so a rebuild is only needed after restoring a backup made before the index existed.
+
## Real-time updates
Mailflare uses a Durable Object WebSocket hub to notify connected users after an inbound message is stored. Mailbox owners, the domain administrator, and delegated users receive events for mailboxes they can access.
diff --cc drizzle/migrations/meta/_journal.json
index 4b9cba3,514ad9a..d872e63
--- a/drizzle/migrations/meta/_journal.json
+++ b/drizzle/migrations/meta/_journal.json
@@@ -216,8 -216,8 +216,15 @@@
"idx": 30,
"version": "6",
"when": 1788850800002,
+ "tag": "0030_add_message_search_index",
+ "breakpoints": true
++ },
++ {
++ "idx": 31,
++ "version": "6",
++ "when": 1788850800003,
+ "tag": "0031_add_password_reset_and_mfa",
+ "breakpoints": true
}
]
}
diff --cc src/lib/backups/export.ts
index 74c15c2,02cc3d2..6795f96
--- a/src/lib/backups/export.ts
+++ b/src/lib/backups/export.ts
@@@ -1,7 -1,7 +1,7 @@@
import type { DatabaseBackupDocument, DatabaseBackupTable, DatabaseRecord } from "./types";
import { mergeLegacyMessageBodies } from "./utils";
- const BACKUP_TABLES: DatabaseBackupTable[] = ["users", "domains", "mailboxes", "mailbox_access", "contacts", "folders", "api_keys", "messages", "message_attachments", "outbound_jobs", "routing_rules", "webhooks", "webhook_deliveries", "sessions", "audit_logs", "backup_settings", "backups", "app_settings", "license_settings", "email_templates", "calendar_events", "auto_reply_deliveries", "spam_token_stats", "spam_reputation", "spam_feedback", "password_reset_tokens", "mfa_recovery_codes", "login_challenges"];
-const BACKUP_TABLES: DatabaseBackupTable[] = ["users", "domains", "mailboxes", "mailbox_access", "contacts", "folders", "api_keys", "messages", "message_attachments", "outbound_jobs", "routing_rules", "webhooks", "webhook_deliveries", "sessions", "audit_logs", "backup_settings", "backups", "app_settings", "license_settings", "email_templates", "calendar_events", "auto_reply_deliveries", "spam_token_stats", "spam_reputation", "spam_feedback", "mailbox_aliases"];
++const BACKUP_TABLES: DatabaseBackupTable[] = ["users", "domains", "mailboxes", "mailbox_access", "contacts", "folders", "api_keys", "messages", "message_attachments", "outbound_jobs", "routing_rules", "webhooks", "webhook_deliveries", "sessions", "audit_logs", "backup_settings", "backups", "app_settings", "license_settings", "email_templates", "calendar_events", "auto_reply_deliveries", "spam_token_stats", "spam_reputation", "spam_feedback", "mailbox_aliases", "password_reset_tokens", "mfa_recovery_codes", "login_challenges"];
/**
* Tables every backup document must contain. Tables added to BACKUP_TABLES
* after the format shipped are absent from older documents, so they stay
diff --cc src/lib/backups/types.d.ts
index 414bdcf,2518b27..49a9f40
--- a/src/lib/backups/types.d.ts
+++ b/src/lib/backups/types.d.ts
@@@ -1,5 -1,5 +1,5 @@@
export type BackupScheduleType = "daily" | "weekly" | "monthly";
- export type DatabaseBackupTable = "users" | "domains" | "mailboxes" | "mailbox_access" | "contacts" | "folders" | "api_keys" | "messages" | "message_attachments" | "outbound_jobs" | "routing_rules" | "webhooks" | "webhook_deliveries" | "sessions" | "audit_logs" | "backup_settings" | "backups" | "app_settings" | "license_settings" | "email_templates" | "calendar_events" | "auto_reply_deliveries" | "spam_token_stats" | "spam_reputation" | "spam_feedback" | "password_reset_tokens" | "mfa_recovery_codes" | "login_challenges";
-export type DatabaseBackupTable = "users" | "domains" | "mailboxes" | "mailbox_access" | "contacts" | "folders" | "api_keys" | "messages" | "message_attachments" | "outbound_jobs" | "routing_rules" | "webhooks" | "webhook_deliveries" | "sessions" | "audit_logs" | "backup_settings" | "backups" | "app_settings" | "license_settings" | "email_templates" | "calendar_events" | "auto_reply_deliveries" | "spam_token_stats" | "spam_reputation" | "spam_feedback" | "mailbox_aliases";
++export type DatabaseBackupTable = "users" | "domains" | "mailboxes" | "mailbox_access" | "contacts" | "folders" | "api_keys" | "messages" | "message_attachments" | "outbound_jobs" | "routing_rules" | "webhooks" | "webhook_deliveries" | "sessions" | "audit_logs" | "backup_settings" | "backups" | "app_settings" | "license_settings" | "email_templates" | "calendar_events" | "auto_reply_deliveries" | "spam_token_stats" | "spam_reputation" | "spam_feedback" | "mailbox_aliases" | "password_reset_tokens" | "mfa_recovery_codes" | "login_challenges";
export type DatabaseRecord = Record<string, string | number | null>;
export type DatabaseBackupDocument = { format: "mailflare-database-backup"; version: 1; createdAt: string; tables: Record<DatabaseBackupTable, DatabaseRecord[]>; };
diff --cc src/lib/setup/migration.ts
index 398d58d,43d7d8f..b300111
--- a/src/lib/setup/migration.ts
+++ b/src/lib/setup/migration.ts
@@@ -29,7 -29,7 +29,8 @@@ const MIGRATION_NAMES =
"0027_add_domain_sending_intent.sql",
"0028_add_keyboard_shortcuts_setting.sql",
"0029_add_spam_protection.sql",
+ "0030_add_message_search_index.sql",
+ "0031_add_password_reset_and_mfa.sql",
];
const INITIAL_SCHEMA_SQL = `