# Auth limits and errors

## Production checks

- Read a strong Better Auth secret from Server Env at request time. Never put it in Public Env, client code, Agent input, logs, or traces.
- Set a request-aware `baseURL`, use `requestOrigin`, or set `vitehub({ publicUrl })`, so callback and cookie origins match the deployed host. Behind a proxy that ends TLS, also set [`advanced.trustedProxyHeaders`](https://vitehub.dev/docs/auth/configure#behind-a-proxy-that-ends-tls).
- Store users and sessions: use [`authDatabase()`](https://vitehub.dev/docs/auth/configure#keep-users-in-the-app-database) and apply its migration, use another Better Auth database adapter, or use [`stateless()`](https://vitehub.dev/docs/auth/configure#sessions-without-a-database) for social sign-in without a database.
- Protect app pages, APIs, and the Console endpoint yourself. A session proves who the user is. Your `authorize` callbacks define roles and permissions.
- Keep the automatic `/api/auth/**` route unless the host mounts the handler itself. Use the same `basePath` on server and client.
- A stateless session (no Better Auth `database` or `secondaryStorage` adapter) cannot use [`session.onRefresh`](https://vitehub.dev/docs/auth/configure#session-refresh). Better Auth refreshes its cookie cache without a hook, so a banned user keeps the session until it expires.

## Handle required authentication

When a required session is missing, `authenticated()` throws `ViteHubError` with code `AUTHENTICATION_REQUIRED`. HTTP adapters turn it into `401`. The error serializes only its public message, without cause or stack.

```ts
import { ViteHubError } from 'vite-hub/runtime'

const error = new ViteHubError('AUTHENTICATION_REQUIRED', 'Sign in to use this Agent.')

console.log(error.toJSON())
```

| Code | When |
| --- | --- |
| `AUTHENTICATION_REQUIRED` | `authenticated()` requires a session and none exists. |
| `AUTH_PROVIDER_OPERATION_FAILED` | A default Better Auth request, session, or user lookup fails. A `4xx` refusal of the session read is a refusal instead, see [Read a refusal](https://vitehub.dev/docs/auth/limits-and-errors#read-a-refusal). `details.operation` is `get-auth-for-request`, `get-session`, or `get-user` (from `agentAuth.user()`). Raw provider details stay in `cause`. |
| `AUTH_C0039` | The Definition, or the object that its callback returns, sets the removed `runtime` option. Pass one callback that returns all the options, such as `defineAuth(({ env }) => ({ advanced: { cookiePrefix: 'acme', trustedProxyHeaders: true }, secret: env.auth.secret.unseal() }))`. |
| `AUTH_C0041` | `session.onRefresh` is not a function. |
| `AUTH_C0042` | `session.onRefresh` is set, but Better Auth has no `database` or `secondaryStorage` adapter. |
| `AUTH_C0043` to `AUTH_C0049` | `preset` is not a preset, a `stateless()` combination fails, or `stateless({ maxAge })` is not a positive duration. See [Sessions without a database](https://vitehub.dev/docs/auth/configure#sessions-without-a-database). |
| `AUTH_C0051` | `VITEHUB_ADMINS` is set, but `plugins` has no `admin()` to store the role. See [Give the first admin role](https://vitehub.dev/docs/auth/configure#give-the-first-admin-role). |
| `AUTH_C0052` | `authDatabase()` gets a name that is not one inline string literal. |
| `AUTH_C0053` | `authDatabase()` needs text IDs from Better Auth or a custom function. `serial`, `uuid`, and `false` need another adapter and schema. |
| `AUTH_C0054` | `authDatabase()` needs one fixed Database target. Use one unconditional inline call, and keep the Database out of runtime overrides. |
| `AUTH_C0055` | The Auth Vite plugin cannot read an `additionalFields` entry. Write it inline in `server/auth.ts`, with a literal `type`. |
| `AUTH_C0056` | The options need a table or a column that the Database of `authDatabase()` does not have, a field conflicts with a column, or a model or field mapping renames a table or a column. Import the plugin by name from its package, such as `import { admin } from "better-auth/plugins"`, and call it in `server/auth.ts` or a file it imports with a relative path, or add its tables to the Database Definition, then run `vitehub database generate`. |
| `AUTH_C0057` | `VITEHUB_ADMIN_EMAILS` is set. It is removed: list the admin's social account in `VITEHUB_ADMINS`. |
| `AUTH_C0058` | A `VITEHUB_ADMINS` entry is an email, has no provider or account id, or is a GitHub login instead of the numeric id. |
| `AUTH_C0059` | `VITEHUB_ADMIN_CLAIM` is not `sha256:` and 64 hex characters. Set the value that `vitehub auth claim` prints, never the code. |
| `AUTH_C0060` | Both `shared/abilities.ts` and another `shared/abilities.*` file exist. Keep one. |
| `AUTH_C0061` | A callback returns `database: true` or `{ name }`. Return `authDatabase()` or another Better Auth adapter. See [Storage adapters](https://vitehub.dev/docs/auth/configure#storage-adapters). |
| `AUTH_C0062` | A callback returns `secondaryStorage: true` or `{ store }`. Return a Better Auth `secondaryStorage` adapter. |
| `AUTH_C0063` | A production build or server has an Auth Definition without `database` or `preset`. Set `database: authDatabase()`, use `preset: stateless()`, or set `database: "memory"` to keep users in one process. |
| `AUTH_C0064` | `access.public` is not an inline array of route strings that start with `/`. |
| `AUTH_C0065` | `access.policy` has no `allow` function. A `recheck` that is not a duration such as `'5m'` fails with `RUNTIME_DURATION_INVALID`. |
| `AUTH_C0066` | `access.policy` is set, but Better Auth has no `database` or `secondaryStorage` adapter, so it cannot end a session. |
| `AUTH_C0067` | `forwardAuth()` has no `header` or no `verify` function. |
| `AUTH_C0068` | An `access.routes` entry uses route pattern syntax, such as `/api/users/:id`, `/api/admin/*`, or the encoded `/api/users/%3Aid`, or a `.` or `..` segment, such as `/api/x/../billing`. Access routes match literal paths. Use a final `/**`, such as `/api/users/**`, and check the parameter in the handler. |
| [`AUTH_B0002`](https://vitehub.dev/docs/auth/limits-and-errors#auth_b0002) | A route rule caches or prerenders a route that `access.routes` guards. |
| `AUTH_R0019` | `authDatabase()` finds no Better Auth tables. Write the call inline in `defineAuth()`, turn on the Database integration, and run `vitehub database generate`. |
| `AUTH_R0020` | A check names an ability that `shared/abilities.ts` does not define. |
| `AUTH_R0021` | An ability rule returned a Promise. Rules also run in the browser, so load the data first and pass it to the rule. |
| `AUTH_R0022` | `vitehub({ publicUrl })` gives several origins and the request host is none of them. Auth does not take its base URL from the request Host. |
| `AUTH_R0023` | The `verify` function of `forwardAuth()` returned a value that is neither `null` nor an identity with `providerId`, `accountId`, `email`, `name`, and `emailVerified`. |
| `AUTH_C####`, `AUTH_B####`, `AUTH_R####` | Missing APIs, malformed responses, and invalid Auth Definitions, `authenticated()` config, or custom callback results. |

Existing ViteHub errors and structural `AbortError` objects keep their identity.

## Read a refusal

The guards refuse a request with one JSON body: `withAuth()`, `withAuthorization()`, `withAnonymousAuthorization()`, `requireAuth()`, `requireAuthorization()`, and the checks of `access.routes`. The body uses H3's error format, so a returned refusal and a thrown H3 error have the same `status`, `statusText`, `message`, and `data`. Nitro and Nuxt add `error: true` to a thrown error, and a stack in development.

```json
{ "status": 401, "statusText": "Unauthorized", "message": "Unauthorized.", "data": { "code": "SIGNED_OUT" } }
```

| `data.code` | Status | When |
| --- | --- | --- |
| [`SIGNED_OUT`](https://vitehub.dev/docs/auth/limits-and-errors#signed_out) | `401` | The request has no Auth Session. |
| [`FORBIDDEN`](https://vitehub.dev/docs/auth/limits-and-errors#forbidden) | `403` | The caller is signed in, but `authorize` does not return `true`. |

Read the code, not the message, in client code. The message can change.

```ts [app/utils/delete-post.ts]
export async function deletePost(id: string): Promise<'deleted' | 'sign-in' | 'forbidden'> {
  const response = await fetch(`/api/posts/${id}`, { method: 'DELETE' })
  if (response.ok) return 'deleted'
  const body: { data?: { code?: string } } = await response.json()
  if (body.data?.code === 'SIGNED_OUT') return 'sign-in'
  if (body.data?.code === 'FORBIDDEN') return 'forbidden'
  throw new Error(`Delete failed with HTTP ${response.status}.`)
}
```

- An HTML `GET` that `withAuth()` or an access route refuses still gets the `access.signIn` redirect. When `withAuthorization()` or an access route refuses it with `403`, it gets the text `Forbidden.`. `requireAuth()` and `requireAuthorization()` still put the code in `data` of their error.
- A `Response` that `authorize` returns goes to the client as it is, without a code.
- When a Better Auth plugin refuses the credentials of the request while Auth reads the session, the guards answer with the plugin's status, code, and headers instead of `SIGNED_OUT`: `withAuth()` and the other wrappers return that refusal, and `requireAuth()`, `requireAuthorization()`, and `requireAbility()` throw it as an H3 `HTTPError`. For example, the API Key plugin refuses an unknown key with `401` and a key that is too short with `403`, both with `data.code` `INVALID_API_KEY`. A Better Auth error with a `5xx` status stays `AUTH_PROVIDER_OPERATION_FAILED`.

## SIGNED_OUT

The request has no Auth Session, or the session ended. Sign in, then send the request again with the session cookie. A `fetch()` from another origin must send `credentials: 'include'`. After a failed sign-in, the URL has `auth_error`, and an HTML `GET` gets the text `403` `Unauthorized.` instead of a new redirect. `requireAuth()` then throws `403` with `SIGNED_OUT`.

## FORBIDDEN

The caller is signed in, but the `authorize` callback of the guard or the access route returned `false` or another value that is not `true`. An access route that has `authorize: true` in its route metadata also refuses when its callback is not there at runtime. Check the callback, or the roles of the user.

## AUTH_B0002

A route rule caches or prerenders a route that `access.routes` guards. The build stops before Nitro bundles the server, and the development server does not start. A cached or prerendered response is made for one visitor and then served to other visitors. A visitor can get the response for an anonymous visitor, or the page of another user.

The check reads the route rules of Nitro after every module, the Nuxt config, and every `nitro:build:before` hook add them. `swr`, `cache`, `isr`, `prerender`, and `static` cache a response. The message names the route rule, the rule that caches, the request method, and the access route that guards it. The check compares route patterns, so a rule counts when it caches at least one guarded path for `GET` or `HEAD`. A wider rule also counts: `'/**': { prerender: true }` covers a guarded `/dashboard/**`. A narrower rule also counts: `'/dashboard/:id(\\d+)': { swr: 60 }` caches a part of a guarded `/dashboard/**`. The check reads an access route as the access middleware does: only a trailing `/**` is a wildcard, and all other characters, such as `:`, `*`, and `(`, are literal.

A rule that turns the cache off must match every guarded path. `'/dashboard': { cache: false }` does not turn off `'/dashboard/**': { swr: 60 }` for `/dashboard/report`. Set the rule on the guarded pattern, for example `'/dashboard/**'`.

In development, a route rule update that caches a guarded route logs `AUTH_B0002` as a warning. The development server keeps the new route rules and continues to serve them. The next build fails.

To fix the build, do one of these:

- Remove the rule that caches from the route rule that the message names.
- Add a route rule for the guarded path that turns the cache off, for example `'/dashboard/**': { cache: false, prerender: false }`.
- If the page is public, remove the route from `access.routes`.

An access route with a `method` other than `GET` or `HEAD` does not stop the build, because Nitro caches only `GET` and `HEAD` requests. The check does not read pages that `definePageMeta({ auth })` guards. [`AUTH_GUARDED_PAGE_CACHED`](https://vitehub.dev/docs/auth/diagnostics/auth_guarded_page_cached) checks them.

## AUTH_CONSOLE_INVALID_REQUEST

A field value, filter, or page size is not valid. Use the field type from `describe()` and a page size from 1 to 100.

## AUTH_CONSOLE_FORBIDDEN

App policy or the Better Auth session denies the action. `describe(request)` gives the reason. Console access does not give app admin rights.

## AUTH_CONSOLE_NOT_FOUND

The record or plugin is missing. Refresh the list and check the plugins in the Auth Definition.

## AUTH_CONSOLE_UNAVAILABLE

The operation needs a database adapter or plugin API. Add it to the primary Auth Definition.

## AUTH_CONSOLE_PROVIDER_FAILED

Better Auth or a computed column reader failed. Read the error cause in server code, and never send it to clients.

## AUTH_MCP_OAUTH_MISSING

`serveMcpOAuthDiscovery()`, `verifyMcpRequest()`, or `deleteUnusedMcpClients()` got an Auth whose plugins have no `jwt()` and `oauthProvider()` with `resources`. An `oauthProvider()` with `disableJwtPlugin` issues opaque access tokens, which these helpers do not check. Add `...mcpOAuth({ resource, loginPage })` from `vite-hub/auth/mcp` to the `plugins` of `defineAuth()`. See [Sign MCP clients in](https://vitehub.dev/docs/auth/configure#sign-mcp-clients-in).

## AUTH_MCP_TOKEN_MISSING

`verifyMcpRequest()` got a request without an `Authorization: Bearer` header. Answer `401` with `WWW-Authenticate: Bearer resource_metadata="<origin>/.well-known/oauth-protected-resource/mcp"` so the MCP client starts the OAuth flow.

## AUTH_MCP_TOKEN_INVALID

The access token fails a check on its signature, issuer, audience (the resource), or expiry, or its user is gone or banned. It also fails on a path that is not a resource of `mcpOAuth()`, unless `verifyMcpRequest()` gets `resource`. Answer `401` with `error="invalid_token"` so the client signs in again.

`error.data.reason` names the check that failed, for a log or a metric. After `error.code === "AUTH_MCP_TOKEN_INVALID"`, TypeScript types `error.data` as `McpTokenInvalidData` for both `verifyMcpRequest()` and `requireCaller()`. The reason is one of `mcpTokenInvalidReasons` from `vite-hub/auth/mcp`:

| Reason | The token |
| --- | --- |
| `expired` | is past its `exp` |
| `malformed` | has a bad signature, issuer, or claim, or is not a JWT |
| `wrong_resource` | names another resource, or the path is not a resource of `mcpOAuth()` |
| `no_keys` | cannot be checked: the Auth has no signing keys |
| `sender_constrained` | is bound to a key, such as a DPoP token |
| `user_gone` | names a user that is deleted or banned |
| `not_allowed` | names a user that `access.policy` or `VITEHUB_MEMBERS` does not allow |
| `client_disabled` | names an OAuth client that is deleted or disabled |
| `session_ended` | names a session that ended, without `offline_access` |

`error.data.clientId` is the OAuth client id once the signature and the claims pass, and `undefined` before.

## AUTH_MCP_CLEANUP_FAILED

`deleteUnusedMcpClients()` could not list or delete OAuth clients. Read the database error in `error.cause`, then run the cleanup again.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/auth.md)
- [Get started](https://vitehub.dev/raw/docs/auth/get-started.md)
- [Configure](https://vitehub.dev/raw/docs/auth/configure.md)
- [Server API](https://vitehub.dev/raw/docs/auth/server-api.md)
- [Hosts](https://vitehub.dev/raw/docs/auth/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/auth/limits-and-errors.md) (this page)
- [Console](https://vitehub.dev/raw/docs/auth/console.md)
- [Event log](https://vitehub.dev/raw/docs/auth/events.md)
- [Abilities](https://vitehub.dev/raw/docs/auth/abilities.md)
- [Protect pages](https://vitehub.dev/raw/docs/auth/protect-pages.md)

Next: [Console](https://vitehub.dev/raw/docs/auth/console.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
