> ## Documentation Index
> Fetch the complete documentation index at: https://docs.theauth.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Upgrading to persistent sessions

> Add "keep me signed in" to a running app, what changes once it is on, rolling it out, turning it off and downgrading.

Upgrading the packages changes nothing for your users until you set `auth.session.persistent`. Without it there are no new routes, cookies or tables, and error bodies stay as they were.

## 1. Upgrade the packages

Move `@glinr/theauth`, and `@glinr/theauth-client` and `@glinr/theauth-react` if you use them, to the releases that include persistent sessions. Upgrade `@glinr/theauth-cli` with them to get the doctor checks.

## 2. Check your session lifetime

Startup fails when `persistent.idleTimeoutMs` is not longer than the session `maxAge`:

```text theme={"dark"}
TheAuth: auth.session.persistent.idleTimeoutMs must be longer than the session maxAge (2592000 seconds)
```

`maxAge` defaults to 7 days and `idleTimeoutMs` to 30. If you raised `maxAge` to keep people signed in, lower it now, since the remember cookie takes over that job. A day or less is a reasonable starting point.

## 3. Apply migration 0016

It creates `theauth_session_families` and `theauth_session_family_tokens` and changes no existing table. It runs only when `auth.session.persistent` is set.

| Setup | What to do |
| - | - |
| Default | Nothing. `createTheAuth` applies it at startup. |
| `database.skipMigrations: true` | Run `theauth db up` with your config in the deploy step, or apply `theauth db sql --dialect <sqlite\|postgres\|mysql> --from 0016`. Otherwise startup throws `SchemaOutdatedError`. |
| Cloudflare D1 | Apply `theauth db sql --dialect sqlite --from 0016` with wrangler before you deploy. |

SQL Server users run `applyMssqlSchema`, which creates the two tables. Prisma users also [add the two models](https://docs.theauth.dev/persistent-sessions-adapters.md#prisma) to their schema.

## 4. Know what changes once it is on

| Area | Behavior with persistent sessions on |
| - | - |
| `rememberMe: true` | No longer gives the session the longer `rememberMeMaxAge` lifetime. The session keeps `maxAge`, and the remember cookie carries the user. `rememberMe: false` still produces a browser session cookie. |
| Sign-in routes | Accept `remember` (or `rememberMe`) and may set two more cookies. |
| 401 responses | Carry the code `session_expired` when a remember cookie is present. |
| Sign-out | Also ends the device, and answers 200 when only the remember cookie is left. |
| Password change, reset, TOTP removal | End the user's remembered devices. |
| `admin.unbanUser` and SCIM reactivation | Fail, and the user stays disabled, if their remembered devices cannot be ended. |
| SCIM `PATCH` with `active: false` that leaves the ban flag off | Answers with an error if remembered devices cannot be ended, so the identity provider retries. Sessions and MCP access are still revoked on that attempt. |
| A new sign-in in the same browser | Ends the device that browser remembered before. |
| `maxConcurrent` | Refreshed sessions count against it. |

If you delete session rows yourself, with SQL or your own scripts, the device behind them can still refresh. Call `theauth.persistentSessions.revokeBySession` or `revokeAll` instead.

## 5. Configure

```typescript theme={"dark"}
auth: {
  session: {
    secret: process.env.THEAUTH_SESSION_SECRET!,
    maxAge: 60 * 60 * 24,
    ...(process.env.PERSISTENT_SESSIONS === 'on' && {
      persistent: { secret: process.env.THEAUTH_REMEMBER_SECRET! },
    }),
  },
},
```

Before you deploy, check:

* Every instance gets the same remember secret.
* Behind a TLS terminating proxy, `trustedProxy` is set. Without it an `https` base URL makes the refresh route answer `insecure_transport`.
* A frontend on another origin is listed in the handler's `trustedOrigins`. The remember cookie is `SameSite=Lax`, so that origin must be on the same site as the auth routes.

## 6. Update the client

Without this step the checkbox has no visible effect.

* With `@glinr/theauth-client` or `@glinr/theauth-react`, pass `{ remember }` to `signIn` and send your API calls through the store's `fetch`. See [client and React](https://docs.theauth.dev/persistent-sessions-client.md).
* With your own client, refresh once and retry once on `session_expired`, and never send users to sign-in on a network error, 429 or 5xx.
* Server rendered pages call `refreshFromRequest` in the page gate, or the [`refreshPageSession`](https://docs.theauth.dev/persistent-sessions-adapters#page-gates) of your framework adapter.
* A Next.js app using `@glinr/theauth-nextjs-auth` sets [`persistent`](https://docs.theauth.dev/persistent-sessions-adapters#nextjs-app-with-a-theauth-backend).
* With `createTheAuthGoClient`, pass `remember` to `login`, `signup` or `passkeys.login`.

## 7. Add a device list

Build a "your devices" screen on `GET /auth/session-families`, with rename, sign out and forget. It differs from a sessions list: a remembered device whose session has expired still appears, with `live: false`.

## 8. Roll it out

1. Deploy with migration 0016 applied and the flag off.
2. Ship the client change. It does nothing while the feature is off.
3. Turn the flag on in staging. Sign in with the box ticked, delete the session cookie in the browser, reload, and confirm one refresh followed by the retried request.
4. Turn it on in production. To limit it to a group of users first, return `false` from `eligible` for everyone else.
5. Watch for `session.reuse_detected`. A steady stream usually means a client or proxy drops `Set-Cookie` headers or restores old cookies.

Run [`theauth doctor`](https://docs.theauth.dev/cli-tools) against the config to catch very long lifetimes.

## Turn it off again

Remove `auth.session.persistent` and deploy. The refresh and device routes disappear, remember cookies are ignored, and users sign in again when their current session ends. Sessions already issued stay valid until they expire.

The rows stay in the database, and revocations made while the feature is off do not update them. If you may turn it back on, empty both tables first, or old remember cookies that have not expired can work again:

```sql theme={"dark"}
DELETE FROM theauth_session_family_tokens;
DELETE FROM theauth_session_families;
```

## Downgrade

Turn the feature off first, then roll the packages back.

* Migration `0016` is additive, so an older version of `@glinr/theauth` runs against the migrated database. It ignores both tables, and `theauth db status` lists `0016` as unknown to that version. This does not stop startup.
* Remove the `persistent` key from your config if the older version's types reject it.
* A session that was created by a refresh may show an extra `theauthRefreshed` key in `session.metadata` on the older version until it expires.
* Client packages can be rolled back on their own. An older client does not refresh, so users sign in again when their session ends.

You do not need to drop the tables. If you do, also delete the ledger row. Without that, a later upgrade treats `0016` as applied and does not recreate them:

```sql theme={"dark"}
DROP TABLE theauth_session_family_tokens;
DROP TABLE theauth_session_families;
DELETE FROM theauth_schema_migrations WHERE id = '0016';
```

Dropping the tables signs every remembered device out for good.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.