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

# 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](https://dotpress.dev/auth/mfa.md) by email or SMS. Dotpress never sends anything itself: it produces messages, and `messages.send` delivers them.

```ts
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

| `type`                     | To                                                                                                                                                                                                                                                                        | Fields                                                                                                                                                                                            |
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `email_verification`       | the address to verify                                                                                                                                                                                                                                                     | `url`, `token`, `expiresAt`                                                                                                                                                                       |
| `sign_up_existing_account` | an address that signs up again                                                                                                                                                                                                                                            | `resetUrl` (with `passwordReset`)                                                                                                                                                                 |
| `password_reset`           | the address of the account                                                                                                                                                                                                                                                | `url`, `token`, `expiresAt`                                                                                                                                                                       |
| `password_changed`         | the address of the account                                                                                                                                                                                                                                                | —                                                                                                                                                                                                 |
| `account_claimed`          | the 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 removed | `passwordRemoved`: a password login needs a [password reset](https://dotpress.dev/auth/password-reset.md). See [Unverified accounts](https://dotpress.dev/auth/magic-link.md#unverified-accounts) |
| `email_change`             | the new address                                                                                                                                                                                                                                                           | `url`, `token`, `expiresAt`                                                                                                                                                                       |
| `email_change_requested`   | the current address, with `emailChange.notifyOldAddress`                                                                                                                                                                                                                  | `newEmail`                                                                                                                                                                                        |
| `email_changed`            | the old address                                                                                                                                                                                                                                                           | `newEmail`                                                                                                                                                                                        |
| `magic_link`               | the address to log in                                                                                                                                                                                                                                                     | `url`, `token`, `expiresAt`; `userId: null` for an address without account (`magicLink.signUp`)                                                                                                   |
| `mfa_code`                 | the address, or the phone number with `channel: 'sms'`                                                                                                                                                                                                                    | `channel` (`email`, `sms`), `code`, `purpose` (`login`, `step_up`, `enrollment`), `expiresAt`: a code of [MFA](https://dotpress.dev/auth/mfa.md)                                                  |

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.timing`          | `send` is called                                        | A failure                                                                                                                                                                        |
| -------------------------- | ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `'after_commit'` (default) | after the step is committed                             | is 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 `provider` | fails 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`).

## 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.

| Type                 | Default path                    | API 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](https://dotpress.dev/auth/data-provider.md#the-stores), 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](https://dotpress.dev/auth/oauth.md) and [SSO](https://dotpress.dev/auth/sso.md) 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](https://dotpress.dev/services/mailer.md), the SMS codes of [`mfa.sms`](https://dotpress.dev/auth/mfa.md) with an SMS sender. Each message type has a minimal template, in English:

```ts
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}`) },
    },
  }),
})
```

| Channel | Messages                                       | Default template                                                             | Data                                                                                                                                                           |
| ------- | ---------------------------------------------- | ---------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `email` | every 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 |
| `sms`   | `mfa_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](#timing).

## In tests

`createMessageOutbox()` of `@dotpress/auth/testing` keeps the messages: a test reads the token a user would receive.

```ts
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')!
```
