Account mail after commit: matched by its token, not by the address (P-4)

holdAccountMailFor bound the next message to an address to whichever
request held it: two resets for one account at once could lose the first
one's mail on the second's rollback, and a form's mail to that address was
deferred to the reset's transaction.

Payload calls generateEmailSubject({ req, token, user }) right before it
sends the reset or verify mail, and the mail carries that token. The plugin
now wraps that function on every auth collection (a project's own one is
kept, Payload's subject otherwise) and holds the token for the request's
transaction; mailAdapter defers only a message to that account alone whose
body contains a held token. An AsyncLocalStorage mark set in a
beforeOperation hook was not used: Payload awaits the hook, and a mark set
inside it does not reach the send (relay/kinds.ts says the same).

Building blocks renamed with it: holdAccountMail(token, address, req),
releaseAccountMail(token). Both review rows are tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-26 01:36:17 +01:00
co-authored by Claude Opus 5.5
parent 4efadb21d5
commit 919953e65e
8 changed files with 151 additions and 67 deletions
+4 -1
View File
@@ -498,7 +498,10 @@ Password reset and e-mail verification go through Payload's built-in mechanism (
account mail ("What a message is"). Payload sends them inside the operation's database transaction; `mailAdapter()`
sends them right **after** it commits instead (still in the same request; nothing when it is rolled back), so a slow
mail server holds no database connection — `mail: { accountMailAfterCommit: false }` keeps them inside
([deployment.md](./deployment.md) §4.3a). An e-mail adapter of your own does the same by calling
([deployment.md](./deployment.md) §4.3a). The message is matched to its request by the token it carries (and the
account's address), never by the address alone: a form's mail to the same address goes out as usual. A project's own
`forgotPassword.generateEmailSubject` / `verify.generateEmailSubject` is kept; an own `generateEmailHTML` must put the
token in the body (any working reset link does), or that mail is sent inside the transaction as before. An e-mail adapter of your own does the same by calling
`sendAccountMailAfterCommit(message, () => send(message))` first.
## Masking secrets in the panel (MaskedField)