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

# Password reset and change

## Reset

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

```ts
passwordReset: {
  ttl: '1h', // lifetime of the links
  revokeSessions: true, // revoke every session of the user
  autoLogin: false, // start a session after the reset
},
```

| Route                        | Body                         | Answer                                                               |
| ---------------------------- | ---------------------------- | -------------------------------------------------------------------- |
| `POST /auth/password/forgot` | `{ email, redirectTo? }`     | `202 { status: 'accepted' }`, always                                 |
| `POST /auth/password/reset`  | `{ token, password, mode? }` | `200 { status: 'password_reset' }`, plus the tokens with `autoLogin` |

- `/auth/password/forgot` sends a `password_reset` link to the address of an account, and answers the same for an unknown address: such a request waits for the running average time of the requests for known addresses. A new link invalidates the previous ones. `3` requests per hour per address and `10` per IP address (`rateLimits.emailRequests`).
- A link is bound to the address it was sent to: after an email change, it answers `400 INVALID_TOKEN`.
- `/auth/password/reset` checks the password policy first: `400 WEAK_PASSWORD` leaves the link usable. Then it consumes the link, sets the hash (`users.setPasswordHash()`), revokes every session of the user (`password_reset`, with `revokeSessions`, the default), and sends `password_changed`.
- An unknown, expired or used link answers `400 INVALID_TOKEN`; `10` checks per 15 minutes per IP address (`rateLimits.tokenChecks`).
- `/auth/password/forgot` answers `403 SSO_REQUIRED`, with `data.connectionId`, for an address of a domain that logs in with its IdP ([SSO enforcement](https://dotpress.dev/auth/sso.md#discovery-and-enforcement)).
- With `mode: 'cookie'`, `/auth/password/reset` from an untrusted origin answers `403 CROSS_ORIGIN_REQUEST` (see [CSRF](https://dotpress.dev/auth/cookies.md#protection-against-cross-site-request-forgery)).
- A reset invalidates every pending link and challenge of the account: email verification, password reset, email change, [magic link](https://dotpress.dev/auth/magic-link.md), handoff code and MFA challenge.
- Events: `password.reset_requested` (existing accounts only), `password.reset` and `password.changed` (`via: 'reset'`).

### Unverified accounts

A reset link proves the address. On an unverified account, the reset first claims it for the owner of the address, like a [magic link](https://dotpress.dev/auth/magic-link.md#unverified-accounts): the password, the passkeys, the linked provider accounts, the second factors, the recovery codes, the trusted devices and the sessions set before are removed, the address is verified (`users.markEmailVerified()`), and an `account_claimed` message is sent. The new password is set after.

## Change

`POST /auth/password/change` is on when the data provider has `users.setPasswordHash()` (and `users.findAccount()`).

```http
POST /auth/password/change
Authorization: Bearer …
{ "currentPassword": "…", "newPassword": "…" }

200 { "status": "password_changed" }
```

| Answer                        | When                                                                                                                                                                          |
| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `401 INVALID_PASSWORD`        | Wrong current password. 10 failures per 15 minutes per user (`rateLimits.stepUp.user`), then `429 TOO_MANY_REQUESTS`.                                                         |
| `400 WEAK_PASSWORD`           | Against the policy, or `rule: 'same_as_current'`                                                                                                                              |
| `409 PASSWORD_NOT_SET`        | An account without password (SSO)                                                                                                                                             |
| `403 IMPERSONATION_FORBIDDEN` | An [impersonation](https://dotpress.dev/auth/sessions.md#impersonation) session                                                                                               |
| `403 MFA_REQUIRED`            | With [`mfa`](https://dotpress.dev/auth/mfa.md#password-change), a user with factors or passkeys without a second factor in the last 10 minutes (`password.changeRequiresMfa`) |

The other sessions of the user are revoked (`password_changed`); the current one stays, with the [sessions store](https://dotpress.dev/auth/sessions.md#the-sessions-store). Without it, sessions cannot be singled out: every session is revoked, the current one included. `password_changed` is sent when `messages` is set. A change or a reset revokes the [trusted devices](https://dotpress.dev/auth/mfa.md#trusted-devices) of the user, and invalidates its pending links and challenges.

`rateLimits.stepUp` counts the failures, with the `peek()` method of the rate limit store, under a ceiling of 30 attempts per 15 minutes: `createAuth()` rejects a store without `peek()`, unless `rateLimits.stepUp.user` counts attempts only.

## Services

`auth.passwordReset.request({ email })`, `auth.passwordReset.complete({ token, password })` and `auth.passwords.change({ userId, currentPassword, newPassword, sessionId?, mfaVerifiedAt? })` do the same outside the routes.
