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

# Email verification and change

## Verification

`emailVerification` sends a link at sign-up, and adds two routes. It needs [messages and links](https://dotpress.dev/auth/messages.md), and the `users` (`findAccount`, `findByEmail`, `markEmailVerified`, `setPasswordHash`) and `tokens` (with `invalidate`) [stores](https://dotpress.dev/auth/data-provider.md#the-stores), plus the methods that claim an unverified account in the other stores the data provider defines (see [Magic link](https://dotpress.dev/auth/magic-link.md#unverified-accounts)).

```ts
emailVerification: {
  required: true, // default false
  ttl: '24h', // lifetime of the links
  autoLogin: false, // start a session once verified
},
```

| Route                     | Body                                         | Answer                                                                                                                                                        |
| ------------------------- | -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `POST /auth/email/verify` | `{ token, codeVerifier?, password?, mode? }` | `200 { status: 'verified' }`, with `passwordRemoved: true` when the account was [claimed](#binding-the-link-to-the-browser), plus the tokens with `autoLogin` |
| `POST /auth/email/resend` | `{ email, redirectTo? }`                     | `202 { status: 'accepted' }`, always                                                                                                                          |

- `users.markEmailVerified(userId, at)` marks the address verified, and activates an `unverified` account: run the activation of your app there, in the transaction of the step. A `suspended` or `disabled` account keeps its status.
- A link is used once. A new link invalidates the previous ones. A link of a previous address does not verify the current one.
- The verification revokes the sessions opened before it (reason `email_verified`): they belong to whoever typed the address, not to its proven owner. It also invalidates the pending email changes, handoff codes and MFA challenges of the account.
- Without the verifier of the sign-up or the password, the verification of an account with a password claims it: see [Binding the link to the browser](#binding-the-link-to-the-browser).
- `/auth/email/resend` sends a link to unverified addresses only, and answers the same for every address: a request for an unknown or verified address waits for the running average time of the requests that send a link. `3` requests per hour per address and `10` per IP address (`rateLimits.emailRequests`).
- An unknown, expired or used link answers `400 INVALID_TOKEN`; `10` checks per 15 minutes per IP address (`rateLimits.tokenChecks`).
- With `mode: 'cookie'`, `/auth/email/verify` from an untrusted origin answers `403 CROSS_ORIGIN_REQUEST` (see [CSRF](https://dotpress.dev/auth/cookies.md#protection-against-cross-site-request-forgery)).

With `required: true`, a password login of an account whose status is `unverified` answers `403 EMAIL_NOT_VERIFIED`, after a valid password only: the state of an account is never revealed to who does not know its password. The links sent by email prove the address: they are not blocked.

The status comes from `users.findAccount()`, or from the `status` returned by `findUserIdentifiers()`. An account created by `users.create()` with `emailVerified: false` has the status `unverified`.

### Binding the link to the browser

The link proves the address, not the password: anyone can sign up with the address of someone else, and wait for its owner to click the link. PKCE ([RFC 7636](https://www.rfc-editor.org/rfc/rfc7636), S256) tells whether the browser that opens the link is the one that chose the password:

```ts
// At sign-up: keep the verifier in the browser.
const verifier = base64url(crypto.getRandomValues(new Uint8Array(32)))
const challenge = base64url(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(verifier)))
localStorage.setItem('sign-up-verifier', verifier)
await post('/auth/sign-up', { email, password, codeChallenge: challenge })

// On the page of the link:
await post('/auth/email/verify', { token, codeVerifier: localStorage.getItem('sign-up-verifier') ?? undefined })
```

- With the matching `codeVerifier`, the address is verified and the password kept.
- The `password` of the account proves it too: on another device, or for the link of a resend, the page asks it. A wrong password answers `401 INVALID_PASSWORD`, and the link stays valid (`10` checks per 15 minutes per IP address, `rateLimits.tokenChecks`).
- A `codeVerifier` that does not match the link, without `password`, answers `400 INVALID_CODE_VERIFIER`, and the link stays valid: the browser of the sign-up opened a link it cannot prove, and the page asks the password.
- Without `codeVerifier` nor `password`, the address is verified and the account is claimed by the owner of the address, like by a [magic link](https://dotpress.dev/auth/magic-link.md#unverified-accounts): the password, the credentials and the sessions set before are removed, and an `account_claimed` message is sent. The answer has `passwordRemoved: true`: the page offers to choose a password with a [password reset](https://dotpress.dev/auth/password-reset.md). An account without password is verified as before.
- `/auth/email/resend` sends a link without challenge: anyone can ask it for any address, so it keeps the password only with that password.
- `emailVerification.autoLogin` starts a session in both cases: the link proves the address. A session started at sign-up (`signUp.autoLogin`) is revoked by the verification either way.

## Email change

`emailChange` lets a user change their address, confirmed by a link sent to the new one. It needs [messages and links](https://dotpress.dev/auth/messages.md), and the `users` (`findAccount`, `findByEmail`, `setEmail`) and `tokens` (with `invalidate`) stores.

```ts
emailChange: { ttl: '1h', notifyOldAddress: true, revokeSessions: true },
```

| Route                                             | Body                                          | Answer                            |
| ------------------------------------------------- | --------------------------------------------- | --------------------------------- |
| `POST /auth/email/change` (authenticated)         | `{ newEmail, currentPassword?, redirectTo? }` | `202 { status: 'accepted' }`      |
| `POST /auth/email/change/confirm` (authenticated) | `{ token }`                                   | `200 { status: 'email_changed' }` |

- The request needs the current password, or an authentication of less than 10 minutes (`403 REAUTHENTICATION_REQUIRED`). A wrong password answers `401 INVALID_PASSWORD`, counted by `rateLimits.stepUp`. Impersonation sessions are refused.
- With [`mfa`](https://dotpress.dev/auth/mfa.md), a user with a second factor or a passkey needs one in the last 10 minutes (`403 MFA_REQUIRED`, `data.maxAgeSeconds`; see [step-up](https://dotpress.dev/auth/step-up.md)): the email factor sends its codes to the address of the account, and the links follow it too.
- With `notifyOldAddress` (the default), the current address receives `email_change_requested` at the request, and `email_changed` once it is confirmed.
- Nothing is sent to the new address when another account uses it; the answer is the same.
- The confirmation needs a session of the user who asked the change (`401` without one); impersonation sessions are refused. A link asked by another account answers `400 INVALID_TOKEN` and stays valid: anyone can ask the address of someone else for their own account, and the owner of the address must not confirm it by clicking. On another device, the user logs in, then opens the link.
- The request and the confirmation answer `403 SSO_REQUIRED`, with `data.connectionId`, when the current or the new address belongs to a domain that logs in with its IdP ([SSO enforcement](https://dotpress.dev/auth/sso.md#discovery-and-enforcement)).
- The confirmation calls `users.setEmail(userId, email)`, which sets the verified address and returns `'taken'` when another account took it meanwhile (a unique constraint): `409 EMAIL_TAKEN`.
- The confirmation revokes every session of the user (`email_changed`, with `revokeSessions`, the default) and its [trusted devices](https://dotpress.dev/auth/mfa.md#trusted-devices), and invalidates every pending link and challenge of the account. A [password reset link](https://dotpress.dev/auth/password-reset.md#reset) sent to the old address stops working.
- Revoking a session of the user (`DELETE /auth/sessions/:id`, a refresh token reuse, an admin) or every session (a logout of every device, `revoke-others`) invalidates the pending email changes.
- `503 PROVIDER_UNAVAILABLE` when the link cannot be sent.

## Services

`auth.emailVerification.resend({ email })`, `auth.emailVerification.verify({ token, codeVerifier?, password? })`, `auth.emailChange.request({ userId, newEmail, mfaVerifiedAt? })` and `auth.emailChange.confirm({ token, userId })` do the same outside the routes. `emailChange.request()` does not check the password: the caller does; it checks the second factor with `mfaVerifiedAt`, the last second factor of the session. `emailChange.confirm()` takes the user of the session that confirms.
