For AI agents: the complete documentation index is available at https://dotpress.dev/llms.txt, the full documentation bundle is available at https://dotpress.dev/llms-full.txt, and this page is available as Markdown at https://dotpress.dev/auth/messages.md.

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.

import { createAuth } from '@dotpress/auth'

const auth = createAuth({
  // …
  messages: { send: (message, { locale }) => deliver(message, locale) },
  links: { baseUrl: 'https://app.example.com' },
  redirects: { allowed: ['https://app.example.com'] },
  passwordReset: true,
})

The messages

typeToFields
email_verificationthe address to verifyurl, token, expiresAt
sign_up_existing_accountan address that signs up againresetUrl (with passwordReset)
password_resetthe address of the accounturl, token, expiresAt
password_changedthe address of the account—
account_claimedthe address of an unverified account claimed by its owner (magic link, verification link without the verifier of the sign-up, password reset, OAuth login with linking: 'verified_email', SSO login linked to the account), when a password or a credential was removedpasswordRemoved: a password login needs a password reset. See Unverified accounts
email_changethe new addressurl, token, expiresAt
email_change_requestedthe current address, with emailChange.notifyOldAddressnewEmail
email_changedthe old addressnewEmail
magic_linkthe address to log inurl, token, expiresAt; userId: null for an address without account (magicLink.signUp)
mfa_codethe address, or the phone number with channel: 'sms'channel (email, sms), code, purpose (login, step_up, enrollment), expiresAt: a code of MFA

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

messages.timingsend is calledA failure
'after_commit' (default)after the step is committedis logged; the routes that wait for the delivery answer 503 PROVIDER_UNAVAILABLE: POST /auth/email/change, and the routes that send an MFA code (login, step-up, enrollment)
'in_transaction'inside the transaction of the step, with its providerfails the step: nothing is written, the route answers 503 PROVIDER_UNAVAILABLE

'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).

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.

TypeDefault pathAPI route
email_verification/verify-email?token=…POST /auth/email/verify
password_reset/reset-password?token=…POST /auth/password/reset
email_change/confirm-email-change?token=…POST /auth/email/change/confirm
magic_link/magic-link?token=…POST /auth/magic-link/verify

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:

import { createAuthMessages } from '@dotpress/auth/messages'

createAuth({
  // …
  messages: createAuthMessages({
    mailer, // @dotpress/mailer
    sms, // optional: { send({ to, text }) }, required by mfa.sms
    templates: {
      email: { password_reset: resetTemplate }, // your own, translated
      sms: { mfa_code: ({ code }, { locale }) => (locale === 'fr' ? `Votre code : ${code}` : `Your code: ${code}`) },
    },
  }),
})
ChannelMessagesDefault templateData
emailevery type, mfa_code with channel: 'email'a mail template of @dotpress/mailer (DEFAULT_AUTH_MAIL_TEMPLATES)AuthMailData: { url, expiresAt } for the links (expiresAt as an ISO string), { resetUrl? }, { newEmail }, { code, purpose, expiresAt }, or nothing
smsmfa_code with channel: 'sms'482913 is your login code. Do not share it. (DEFAULT_AUTH_SMS_TEMPLATES)AuthSmsData: { code, purpose, expiresAt }

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.

import { createMemoryDataProvider, createMessageOutbox } from '@dotpress/auth/testing'

const outbox = createMessageOutbox()
const auth = createAuth({ /* … */ messages: { send: outbox.send }, passwordReset: true })

await fetch('/auth/password/forgot', postJson({ email: 'ada@example.com' }))
const { token } = outbox.last('password_reset', 'ada@example.com')!