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

# Persistent sessions in frameworks and Prisma

> Server rendered page gates for each framework adapter, the theAuth backend mode of the Next.js auth adapter, and the Prisma models.

Mounting `createTheAuthHandler` through a framework adapter is enough for the [refresh route](https://docs.theauth.dev/persistent-sessions-api.md#refresh-route) and the device routes. This page covers the code that runs outside those routes: page gates, a Next.js app in front of a separate theAuth backend, and Prisma schemas.

## Page gates

Every framework adapter below exports `refreshPageSession`. It returns the request's live session, or refreshes from the remember cookie, using the framework's own request and cookie types.

| Package | Call | Where the new cookies go |
| - | - | - |
| `@glinr/theauth-nextjs` | `refreshPageSession(auth, request)` | Wrap your response in `withSessionCookies(response, gate)`. |
| `@glinr/theauth-solidstart` | `refreshPageSession(auth, event)` | `withSessionCookies(response, gate)`. |
| `@glinr/theauth-tanstack` | `refreshPageSession(auth, input)` | `withSessionCookies(response, gate)`. |
| `@glinr/theauth-hono` | `refreshPageSession(auth, c)` | Appended to the response. |
| `@glinr/theauth-astro` | `refreshPageSession(auth, Astro)` | `Astro.cookies`. |
| `@glinr/theauth-sveltekit` | `refreshPageSession(auth, event)` | `event.cookies`. |
| `@glinr/theauth-nuxt` | `refreshPageSession(auth, event)` | Appended to the response. |
| `@glinr/theauth-express` | `refreshPageSession(auth, req, res)` | Appended to `res`. |
| `@glinr/theauth-fastify` | `refreshPageSession(auth, request, reply)` | Added to `reply`. |
| `@glinr/theauth-nestjs` | `refreshPageSession(auth, req, res)` | Appended to `res`. |

```typescript theme={"dark"}
import { refreshPageSession } from '@glinr/theauth-express';

app.get('/account', async (req, res) => {
  const gate = await refreshPageSession(theauth, req, res);
  if (!gate.success) {
    if (gate.error.code === 'refresh_unavailable') return res.status(503).send('Try again');
    return res.redirect('/sign-in');
  }
  res.render('account', { userId: gate.data.session.userId });
});
```

The last argument is optional. `trustedOrigins` adds origins that a non-`GET` request may come from. The Express, Fastify, NestJS and Nuxt gates also take `trustProxy`, which believes `X-Forwarded-Proto` and `X-Forwarded-Host`. Set it behind a TLS terminating proxy.

Call a gate only where `Set-Cookie` reaches the browser. A Next.js Server Component cannot set cookies, so use middleware on the Node.js runtime or a route handler. `refreshPageSession` throws when `auth.session.persistent` is not set.

Without one of these adapters, call [`persistentSessions.refreshFromRequest`](https://docs.theauth.dev/persistent-sessions#server-rendered-pages) yourself.

## Next.js app with a theAuth backend

`@glinr/theauth-nextjs-auth` runs in a Next.js app whose backend is a separate server. When that backend is theAuth with persistent sessions, set `persistent`.

```typescript title="auth.config.ts" theme={"dark"}
import { createAuthConfig } from '@glinr/theauth-nextjs-auth';

export const authConfig = createAuthConfig({
  secret: process.env.THEAUTH_SESSION_SECRET!,
  backendUrl: 'https://api.example.com',
  appUrl: 'https://app.example.com',
  persistent: {},
});
```

The endpoints then default to the theAuth routes under `/api/theauth`, and the backend must list `appUrl` in its trusted origins. `persistent.sessionCookie` and `persistent.rememberCookie` name the backend's cookies if you changed them.

* Sign in on the server with `remember: true` in the body. Pass the backend response to `adoptBackendSession(authConfig, response)` in a server action, or to `setSessionCookiesFromBackend(nextResponse, response, authConfig)` in a route handler. The adapter's refresh cookie then holds the backend's remember token. A sign-in without `remember` removes an earlier one.
* `withAuth` is the page gate. It refreshes for page navigations from your own app, never for `/api` routes, subresources or cross-site navigations. If the backend is unreachable, a protected page without a live session answers 503 and nothing is cleared.
* `export const POST = createRefreshRouteHandler(authConfig)` gives client code a refresh route. It answers 401 when the sign-in is over, 403 when refused and 503 on an outage.
* `refreshSession` does nothing in a Server Component, because the rotated cookies could not be saved there.
* Sign-out sends the remember token to the backend, so the device ends even after the session lapsed.

Without the `persistent` option the adapter behaves as before.

## Prisma

`@glinr/theauth-prisma` ships `TheAuthSessionFamily` and `TheAuthSessionFamilyToken`, which match migration `0016`. Copy both models from `node_modules/@glinr/theauth-prisma/src/schema.prisma` into your schema, then:

```bash theme={"dark"}
pnpm prisma migrate dev --name persistent-sessions
pnpm prisma generate
```

On MySQL add `@db.Text` to `userAgent` and `label`. If `createTheAuth` already created the tables, add the models anyway so a later `prisma db push` does not treat them as drift.

The adapter exposes typed methods for the two tables, such as `listSessionFamilies`, `revokeSessionFamily` and `eraseSessionFamilies`. `deleteUser` erases a user's device rows when the models exist. The [adapter is a standalone query layer](https://docs.theauth.dev/prisma.md): refresh and rotation run in `createTheAuth`, so prefer `theauth.persistentSessions` for anything that changes a device. A revocation made through Prisma marks the device row only and leaves its current session running.

A client generated from an older schema still type checks and works. Calling a device method on it throws an error that names the missing models.

## SQL Server

`applyMssqlSchema` creates the two tables on its next run. It alters no existing table.


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