Messages and links
The account features (sign-up, email verification, password reset and change, email change, magic link) reach the users by email, and the codes of MFA by email or SMS. Dotpress never sends anything itself: it produces messages, and messages.send delivers them.
The messages
Every message has type, to and userId. send(message, { provider, locale, meta }) also receives the locale of the request (first language of Accept-Language), the request (meta), and the data provider of the step.
Timing
'in_transaction' fits the outbox pattern: send writes a row in the same transaction as the account, and a worker sends the emails. Nothing is sent for a step that fails, and nothing is lost for a step that succeeds.
The routes that answer the same for every address (/auth/sign-up, /auth/email/resend, /auth/password/forgot, /auth/magic-link) do not wait for the delivery in 'after_commit' mode: a request for an existing account would take longer than for an unknown one, and reveal it. Their failures are logged, not reported. The same goes for the notices sent after a change (password_changed, email_changed).
Links
A link opens a page of the app, with the token in its query string. The page posts the token to the API: a link opened by an email scanner changes nothing.
links.paths changes the paths; links.build({ type, token, redirectTo }) replaces the builder, for a mobile deep link for example.
Tokens are 256 random bits, stored as their SHA-256 hash in the tokens store, and used once. A new token invalidates the previous ones of the same purpose.
Redirects
redirectTo, sent to /auth/sign-up, /auth/email/resend, /auth/password/forgot, /auth/email/change and /auth/magic-link, is added to the link when redirects.allowed accepts it: the same origin, and the same path or a path below it. Other values are dropped, so that a link of your emails never redirects to another site. The social login and SSO flows check their redirectTo against the same list.
With createAuthMessages()
createAuthMessages() of @dotpress/auth/messages delivers every message of the auth: the emails with a mailer, the SMS codes of mfa.sms with an SMS sender. Each message type has a minimal template, in English:
An SMS template is a function (data, { locale }) => text; a mail template is rendered by the mailer. Both use the locale of the request.
The SMS sender is any object with send({ to, text }): the client of an SMS provider, or a function of the app. createAuthMessages() declares its channels (messages.channels): ['email'] without sms, and createAuth() then refuses mfa.sms. A messages.send of the app declares its own with channels; it delivers every channel by default. createAuthMessages({ timing }) sets the timing.
In tests
createMessageOutbox() of @dotpress/auth/testing keeps the messages: a test reads the token a user would receive.