# Auth server API

## Public imports

| Import | Use |
| --- | --- |
| `createDatabaseAuthEventStore` from `vite-hub/auth` | Create the opt-in SQLite or D1 [event log](https://vitehub.dev/docs/auth/events). |
| `getAuthEvents`, `listAuthEvents`, `aggregateAuthEvents` from `vite-hub/auth/server` | Read events and daily counts from server code. |
| `defineAuth` from `vite-hub/auth` | Declare the Primary Auth Definition. |
| `isGitHubOrgMember` from `vite-hub/auth` | Check that a GitHub token belongs to an active [organization member](https://vitehub.dev/docs/auth/server-api#github-organization-membership). |
| `checkGitHubOrgMember` from `vite-hub/auth` | The same check as `[error]`, with the [reason](https://vitehub.dev/docs/auth/server-api#github-organization-membership) when it fails. |
| `stateless` from `vite-hub/auth/presets` | Keep sessions in encrypted cookies, [with no database](https://vitehub.dev/docs/auth/configure#sessions-without-a-database). |
| `hubAuth` from `@vite-hub/auth/vite` | Add Auth discovery, the route, and generated server aliases. |
| `useAuth`, `auth`, `getAuth` from `vite-hub/auth/server` | Use the Better Auth instance in server code. |
| Default export of `vite-hub/auth/server` | The Auth route as a Fetchable, `{ fetch(request) }`. Mount it when you set `route: false`. |
| `handleAuth`, `createAuthHandler` from `vite-hub/auth/server` | Mount or call the Auth handler manually. |
| `withAuth` from `vite-hub/auth/server` | Run a handler only when a session exists. |
| `withAuthorization` from `vite-hub/auth/server` | Run a handler only after `true` or an `authorize` callback accepts the request. Never redirects to sign-in. |
| `requireAuth`, `requireAuthorization` from `vite-hub/auth/server` | Check the session inside a handler. They return the checked session or throw an HTTP error. |
| `requireServiceToken`, `checkServiceToken` from `vite-hub/env` | Check the bearer token of a route that a script or a runner calls. No session and no Auth setup. See [Service tokens](https://vitehub.dev/docs/env/service-tokens). |
| `defineAbilities`, `deny`, `guest` from `vite-hub/auth/abilities` | Write the rules of `shared/abilities.ts`. See [Abilities](https://vitehub.dev/docs/auth/abilities). |
| `requireAbility`, `can`, `AbilityError` from `vite-hub/auth/server` | Check a rule of `shared/abilities.ts` in server code. |
| `useAbility`, `Can`, `Cannot` from `vite-hub/auth/vue` | Check a rule of `shared/abilities.ts` in Vue. `useAbility()` returns `can()`, `cannot()`, `authorize()`, and `ready`. |
| `useUserSession`, `useSession`, `createAuthClient`, `loadAuthClient` from `vite-hub/auth/vue` | Read session state and call Better Auth from Vue. Better Auth loads on first use. |
| `defineAuthClient` from `vite-hub/auth/vue` | Set the [client plugins](https://vitehub.dev/docs/auth/server-api#client-plugins) of the default client in `auth.client.ts`. |
| `authenticated` from `vite-hub/auth/agent` | Turn a Better Auth session into an Agent Invoker (who calls the Agent). |
| `agentAuth` from `vite-hub/auth/agent` | The `auth` primitive that generated Agent routes pass to Capabilities. `agentAuth.user(actor)` reads the Auth user from `actor.meta.authUserId`, or returns `null`. `authenticated()` sets `authUserId` for a request with a session. Channel and anonymous Actors have none, unless your `invoker.resolve` adds it. |
| `getViteHubErrorShape` from `vite-hub/runtime` | Handle missing sessions and provider failures by Auth error code. |

## Use it at runtime

ViteHub mounts the Auth route at `/api/auth/**`, so same-origin apps need no route file.

In Vue, `useUserSession()` gives you `user`, `session`, sign-in and sign-out actions, the `loggedIn`, `pending`, and `ready` refs, and the Better Auth `client`.

```ts [lib/auth-client.ts]
import { useUserSession } from 'vite-hub/auth/vue'

export const userSession = useUserSession()
```

All components share one session state, so a list of 100 rows that call `useUserSession()` makes one Better Auth subscription. `await refresh()` reads the session again, updates every component, and returns the session, or `null` without one. Calls at the same time share one read. On the server, `refresh()` returns the session that the server read for the request. `ready` is `true` after the first read, and stays `true` while a later read runs. In Nuxt, the composables start with the session that the server read, and the browser reads it again only when it can change, such as when the tab shows again or another tab signs out.

In Nuxt, `useUserSession()` and `useSession()` use the Definition's `basePath` below `app.baseURL`, such as `/app/auth` for `basePath: '/auth'` below `/app/`. In the browser, they follow a `NUXT_APP_BASE_URL` that you set at runtime.

In Nuxt, a client call during server rendering, such as `listSessions()` in `<script setup>` or `useAsyncData()`, calls the app in-process with the visitor's cookie. The page renders the data on the server, and a session cookie that Better Auth refreshes reaches the browser with the page. A client with its own `baseURL` or `fetchOptions.customFetchImpl` keeps them, so its server calls do not carry the visitor's cookie. Better Auth's origin check refuses a `POST` call during server rendering with `403`, so make calls that change data in the browser. A prerendered or cached page renders without the visitor's cookie, so its calls answer as signed out.

`signOut()` signs out with Better Auth and sets `user` and `session` to `null` at once, in every component. Pass `redirectTo` to open a page after the sign-out:

```vue [src/components/UserMenu.vue]
<script setup lang="ts">
import { useUserSession } from 'vite-hub/auth/vue'

const { loggedIn, user, signOut } = useUserSession()
</script>

<template>
  <button v-if="loggedIn" @click="signOut({ redirectTo: '/' })">
    Sign out {{ user?.name }}
  </button>
</template>
```

- Nuxt opens `redirectTo` with `navigateTo()`. A plain Vue app loads it with `location.assign()`.
- Without `redirectTo`, in Nuxt, a page with `definePageMeta({ auth: 'user' })` sends the visitor to its login page, as the page guard does. Other pages stay open.
- Better Auth gets the other options, such as `fetchOptions`, and `signOut()` returns its result. When the sign-out fails, the session and the page stay.
- When the server answers with the provider's sign-out page, such as with OpenID Connect logout, `signOut()` opens that page in place of `redirectTo`, but only an `http:` or `https:` page. Better Auth's `callbackURL` option sets the page after that. With `disableRedirect: true`, `signOut()` returns that page in `data.url` and opens `redirectTo`.

To forget data of the person after a sign-out, register a listener with `onSignOut()`. `signOut()` calls it in the browser after the server ended the session, waits for it, and then opens the next page or the provider's sign-out page. It returns a function that stops the listener. With [Sync](https://vitehub.dev/docs/sync/vue#accounts-and-sign-out) in Nuxt, ViteHub adds `onSignOut(() => clearSync())` for you.

```ts [src/main.ts] fragment
import { onSignOut } from 'vite-hub/auth/vue'

onSignOut(() => localStorage.removeItem('drafts'))
```

To sign out from a link or from server code, call `signOut(event)` from `vite-hub/auth/server`. It ends the session and returns the response that clears the session cookies:

```ts [server/routes/logout.get.ts]
import { signOut } from 'vite-hub/auth/server'
import { defineHandler } from 'h3'

export default defineHandler(event => signOut(event, { redirectTo: '/login' }))
```

- With `redirectTo`, the response is a `303` redirect to that page. Without it, the response is `204`. Return it from the route, so the browser gets the cookies.
- If Better Auth returns a provider logout URL, the response redirects there first and passes `redirectTo` as the return URL.
- `redirectTo` must be a path of the app, such as `/login`. Another site fails with `AUTH_SIGN_OUT_REDIRECT_INVALID`.
- A request without a session gets the same response.

### Client plugins

To use Better Auth client plugins, such as `adminClient()`, export their options from `app/auth.client.ts` in a Nuxt app, or from `auth.client.ts` in the Vite root. The default client, `useUserSession()`, `useAuthClient()`, and the Nuxt page guard then use one client with these plugins.

```ts [app/auth.client.ts]
import { adminClient } from 'better-auth/client/plugins'
import { defineAuthClient } from 'vite-hub/auth/vue'

export default defineAuthClient({
  plugins: [adminClient()],
})
```

```vue [app/pages/admin.vue]
<script setup lang="ts">
definePageMeta({ auth: 'user' })

const { client } = useUserSession()
const { data } = await client.admin.listUsers({ query: { limit: 10 } })
</script>
```

- `defineAuthClient()` takes the options of Better Auth's `createAuthClient()`. ViteHub sets `basePath`. In Nuxt, it is the `basePath` of the Auth Definition, below `app.baseURL`.
- The client imports the file together with Better Auth, on first use, not when the app starts.
- ViteHub writes the type of the file to `.vitehub/types/auth.d.ts`, so `client.admin` and the other plugin endpoints are typed. Restart the dev server after you add or remove the file.

For a second client, or a custom `basePath` outside Nuxt, import `createAuthClient` from the same entry. It loads Better Auth on the first call, so it adds nothing to a page that does not sign in. `loadAuthClient(client)` returns Better Auth's own client for members such as `$store`. Without `basePath`, it uses the same path as the default client. A `basePath` that you set is used as it is, so include the app base path, such as `/app/auth`.

The default client has the user and session fields of your Auth Definition. See [Typed user fields](https://vitehub.dev/docs/auth/server-api#typed-user-fields).

Better Auth loads on the first Auth request, or the first request an access route matches. Importing `vite-hub/auth/server` also loads it, so import it in routes, not in a Nitro plugin or global middleware. A cold start then does not compile it.

Server code reads the Better Auth instance of a request with `useAuth(event)`.

```ts [server/api/session.get.ts]
import { useAuth } from 'vite-hub/auth/server'
import { defineHandler } from 'h3'

export default defineHandler(event => useAuth(event).api.getSession({ headers: event.req.headers }))
```

The instance has the endpoints of your Auth Definition's plugins, without a cast. With `admin()` in `server/auth.ts`, this type-checks:

```ts [server/api/users.get.ts] fragment
import { requireAuthorization, useAuth } from 'vite-hub/auth/server'
import { defineHandler } from 'h3'

export default defineHandler(async (event) => {
  await requireAuthorization(event, ({ user }) => user.role === 'admin')
  return useAuth(event).api.listUsers({ query: { limit: 20 }, headers: event.req.headers })
})
```

To protect a route, wrap its handler. The guard checks the request, and your handler runs only if the check passes, with the checked `authorization`. You write no `if`, so a forgotten one cannot run the protected action.

```ts [server/api/profile.get.ts]
import { defineEventHandler } from 'h3'
import { withAuth } from 'vite-hub/auth/server'

export default defineEventHandler(withAuth((event, { user }) => ({ id: user.id })))
```

To check inside the handler, call `requireAuth(event)`. It returns the checked `authorization`, or throws an H3 `HTTPError` that rejects like `withAuth()`: `401`, or the `access.signIn` redirect for an HTML request. `requireAuthorization(event, authorize)` rejects like `withAuthorization()`: `401` without a session, `403` when `authorize` returns `false`, or an HTTP error from the callback's `Response`. The rest of the handler runs only after the check passes.

Every refusal of the guards has one JSON body in H3's error format, with a stable code in `data.code`: `SIGNED_OUT` for `401` and `FORBIDDEN` for `403`. See [Read a refusal](https://vitehub.dev/docs/auth/limits-and-errors#read-a-refusal). For a `Response` from `authorize`, the inline guards also use H3's JSON error format. They keep the response status and headers, except `content-length`, which changes with the body. JSON object fields stay at the top level, and other JSON values become `data`. JSON media types, such as `application/problem+json`, stay unchanged. Text becomes `message` with a JSON content type. Use `withAuthorization()` when the response body must stay unchanged.

```ts [server/api/me.get.ts]
import { requireAuth } from 'vite-hub/auth/server'
import { defineHandler } from 'h3'

export default defineHandler(async (event) => {
  const { user } = await requireAuth(event)
  return { id: user.id }
})
```

## Server helpers

| Helper | Description |
| --- | --- |
| `auth` | Proxy to the discovered Better Auth instance. |
| `getAuth()` | Returns the discovered Better Auth instance. |
| `useAuth(event)` | Returns the Better Auth instance of the app's Auth Definition for a request event or a Web `Request`. |
| `handleAuth(input)` | Handles an Auth HTTP request. `input` is a Web `Request` or a request event with [Standard Context](https://vitehub.dev/docs/reference/runtime-context#standard-context) properties, such as an H3 2 event in a ViteHub app. Auth reads the client address and the Cloudflare bindings from the Standard Context of the Request. A Request that Nitro did not create gets one that reads srvx `ip` and `runtime.cloudflare`. |
| `signOut(event, { redirectTo }?)` | Ends the session of the request. Returns a `303` redirect to `redirectTo`, or `204`, with the cookies that clear the session. |
| `createAuthHandler(definition)` | Creates a handler called as `handler(request, { clientAddress }?)`. Manual hosts pass the peer address for rate limiting and session IPs. |
| `withAuth(handler, definition?)` | Returns a handler that calls `handler(input, authorization)` when a session exists. Else it returns JSON `401` with the code `SIGNED_OUT`, or the sign-in redirect for an HTML request. |
| `withAuthorization(authorize, handler, definition?)` | Returns a handler that calls `handler(input, authorization)` when allowed. Else it returns JSON `401` (`SIGNED_OUT`) without a session, `403` (`FORBIDDEN`) when `authorize` returns `false`, or the callback's `Response`. |
| `withAnonymousAuthorization(authorize, handler, definition?)` | Like `withAuthorization()`, but a signed-out caller also reaches `authorize`, with `session` and `user` set to `null`. `false` returns JSON `401` (`SIGNED_OUT`) for a signed-out caller and `403` (`FORBIDDEN`) for a signed-in one. |
| `requireAuth(event, definition?)` | Returns the checked `authorization` when a session exists. Otherwise it throws an `HTTPError` with the status and headers of the `withAuth()` response. |
| `requireAuthorization(event, authorize, definition?)` | Returns the checked `authorization` when allowed. Otherwise it throws an `HTTPError` with the status and headers of the `withAuthorization()` response. |
| `requireAbility(event, name, ...args)` | Returns `authorization` when the [ability](https://vitehub.dev/docs/auth/abilities) allows the caller. Else throws `AbilityError`, which H3 sends with status `401`, `403`, or the status of `deny()`, and `{ code, details }` in `data`. |
| `can(event, name, ...args)` | Returns `true` when the ability allows the caller, else `false`. |
| `checkAuthMember(userId, event?)` | Checks [`VITEHUB_MEMBERS`](https://vitehub.dev/docs/auth/configure#choose-who-can-sign-in) for a user without a session. See [Check a member without a session](https://vitehub.dev/docs/auth/server-api#check-a-member-without-a-session). |

`useAuth()`, `auth`, and `getAuth()` return the instance typed from your Auth Definition, with the endpoints and fields of its plugins. The Vite plugin registers the type of `server/auth.ts` in `.vitehub/types/auth.d.ts`, so `useAuth(event).api.listUsers` type-checks when the definition uses the `admin()` plugin, without a cast. `AuthFor<typeof definition>` from `vite-hub/auth` names the instance type of another definition.

The `authorization` argument and the `requireAuth()` result are a frozen `{ request, session, user }` object. Only the guards create it. A caller cannot pass its own value to the returned handler.

### GitHub organization membership

`isGitHubOrgMember(accessToken, org, options?)` returns `true` only when GitHub says the token's user is an active member of `org`. The token needs the `read:org` scope. A pending invitation, a non-2xx response, a network error, or a timeout returns `false`, so a failed check never lets a user in. An empty `org` returns `false`. It uses only `fetch` and Web Crypto, so it runs on Node, Workers, and other hosts.

| Option | Default | Use |
| --- | --- | --- |
| `cacheMs` | No cache | Keep a successful check this many milliseconds per token and organization. Failed checks are never kept. Cache keys are SHA-256 digests, not tokens, and expired keys are removed. |
| `timeoutMs` | `5000` | Stop the GitHub request after this many milliseconds. |

```ts [server/utils/members.ts]
import { isGitHubOrgMember } from 'vite-hub/auth'

export const isTeamMember = (token: string | null | undefined) => isGitHubOrgMember(token, 'acme', { cacheMs: 300_000 })
```

The cache is in memory, so each Worker isolate or server process has its own.

`checkGitHubOrgMember(accessToken, org, options?)` runs the same check and returns `[error, undefined]`, so you can tell the user why they cannot get in. It takes the same options. An active membership returns `[undefined, undefined]`, and an empty `org` throws `AUTH_GITHUB_ORG_REQUIRED`.

| `error.code` | When | What to do |
| --- | --- | --- |
| `AUTH_GITHUB_TOKEN_INVALID` | GitHub answers `401`, or there is no token. | Ask the user to sign in with GitHub again. |
| `AUTH_GITHUB_ORG_ACCESS_DENIED` | GitHub answers `403`, or `404` to a classic OAuth token without `read:org`. The organization uses SAML single sign-on that the token is not authorized for, or it restricts OAuth apps. | Ask the user to sign in again with `read:org` and to authorize single sign-on. |
| `AUTH_GITHUB_UNAVAILABLE` | A network error, a timeout, a `429` or `5xx`, a rate limit, or a body that is not a membership. | Answer `503` and ask the user to try again. |
| `AUTH_GITHUB_NOT_ORG_MEMBER` | GitHub answers `404`, or the invitation is pending. | Answer `403`. |

```ts [server/utils/require-team-member.ts]
import { HTTPError } from 'h3'
import { checkGitHubOrgMember } from 'vite-hub/auth'

const statuses = {
  AUTH_GITHUB_TOKEN_INVALID: 401,
  AUTH_GITHUB_ORG_ACCESS_DENIED: 403,
  AUTH_GITHUB_NOT_ORG_MEMBER: 403,
  AUTH_GITHUB_UNAVAILABLE: 503,
}

export async function requireTeamMember(token: string | null | undefined) {
  const [error] = await checkGitHubOrgMember(token, 'acme', { cacheMs: 300_000 })
  if (error) throw new HTTPError({ status: statuses[error.code], message: error.why, data: { code: error.code } })
}
```

### Check a member without a session

Sign-in and every session already check [`VITEHUB_MEMBERS`](https://vitehub.dev/docs/auth/configure#choose-who-can-sign-in). Some requests carry a user id but no session, such as an upload link or a signed review token. Check them with `checkAuthMember()`:

```ts [server/api/uploads/[ticket].put.ts]
import { checkAuthMember } from 'vite-hub/auth/server'
import { kv } from 'vite-hub/kv'
import { defineHandler, getRouterParam, HTTPError } from 'h3'

export default defineHandler(async (event) => {
  // The upload page stored the uploader's user id under the ticket.
  const [, userId] = await kv.get(`upload-tickets:${getRouterParam(event, 'ticket')}`)
  if (typeof userId !== 'string') throw new HTTPError({ status: 404 })
  const [error] = await checkAuthMember(userId, event)
  // During a GitHub outage, ask the uploader to try again.
  if (error?.code === 'AUTH_GITHUB_UNAVAILABLE') throw new HTTPError({ status: 503 })
  if (error) throw new HTTPError({ status: 403 })
  return { ok: true }
})
```

- It uses the rules of sign-in: a verified email or domain, a listed account, the accounts in `VITEHUB_ADMINS`, the admin role, or an active GitHub organization membership.
- It returns `[undefined, undefined]` when the list lets the user in, or when `VITEHUB_MEMBERS` is not set.
- It returns `AUTH_NOT_MEMBER` for a refused or unknown user, and `AUTH_GITHUB_UNAVAILABLE` when GitHub cannot confirm a membership.
- It reuses a confirmed organization membership for five minutes, from the token that the user's GitHub account stores.
- Pass the request event when you have one. On Cloudflare, Auth reads `VITEHUB_MEMBERS` from its bindings.

### Authorize access routes

Add `authorize` when a session alone is not enough. ViteHub calls it only after authentication.

| `authorize` returns | Result |
| --- | --- |
| `true` | The request continues. |
| `false` | `403`. |
| A `Response` | That response, as-is. |

```ts [server/auth.ts]
import { admin } from 'better-auth/plugins'
import { defineAuth } from 'vite-hub/auth'
import { authDatabase } from 'vite-hub/auth/database'

export default defineAuth({
  database: authDatabase(),
  plugins: [admin()],
  access: {
    routes: [
      {
        route: '/api/admin/**',
        authorize: ({ user }) => user.role === 'admin',
      },
    ],
  },
})
```

The callback receives the signed-in `user`, `session`, `request`, and `event`: the input of the guard. For access routes and in a Nitro route it is the H3 event, so `event.context` and functions that take the event, such as `useAuth(event)`, work in the callback. `withAuthorization()` types `event` as the input of the handler it returns, and a direct Fetchable call passes the Request. ViteHub defines no roles. The example reads the `role` that Better Auth's `admin()` plugin stores. Check your own roles or permissions here.

The same callback protects [Blob serve routes](https://vitehub.dev/docs/blob/configure#protect-served-objects) and [Collections](https://vitehub.dev/docs/source/server-api#protect-a-collection). Their generated routes wrap the read with `withAuthorization(authorize, handler)`. Unlike `withAuth()`, it never redirects to sign-in, so image and fetch requests get a status code.

The generated access middleware checks each request that an access route matches. A route ending in `/**` matches its base path and descendants. Exact routes match only that path. A route has no other pattern syntax: `*`, `:name`, `(`, `{`, and `?`, also when percent-encoded, and `.` or `..` segments fail with `AUTH_C0068`. A `GET` route also guards `HEAD`. The middleware reads the path as the Nitro router does, so `/api/billing/` and `/api/%62illing` match `/api/billing`. All matching rules apply. Unmatched requests do not load the Auth Definition or session.

The [Console](https://vitehub.dev/docs/development/console#protect-the-console-route) endpoint does not use these access routes. It takes only signed requests and one-time links.

### Make every route private

With `routes: ['/**']`, the access middleware guards every path. List the paths that a signed-out visitor can open in `access.public`. A request to a public path skips all access routes and their `authorize` callbacks.

```ts [server/auth.ts]
import { defineAuth } from 'vite-hub/auth'

export default defineAuth({
  access: {
    routes: ['/**'],
    public: ['/', '/login', '/api/auth/**', '/api/health'],
    signIn: { provider: 'github' },
  },
})
```

- Write public routes like access routes: an exact path, or a path that ends in `/**`. Write the list inline, so the Vite plugin can read it at build time.
- Keep `/api/auth/**` public. Sign-in and the OAuth callback are below it.
- Make public the files that the browser loads before sign-in, such as `/_nuxt/**` and `/favicon.ico` in a Nuxt app.
- A route rule that caches or prerenders only public paths does not stop the build ([`AUTH_B0002`](https://vitehub.dev/docs/auth/limits-and-errors#auth_b0002)).

### Decide who can stay signed in

`access.policy` decides in code who can use the app. For a list of accounts, emails, domains, or GitHub organizations, set [`VITEHUB_MEMBERS`](https://vitehub.dev/docs/auth/configure#choose-who-can-sign-in) instead. Auth calls `allow` when it creates a session, so a refused person cannot sign in. It calls `allow` again when `recheck` milliseconds have passed for a session, on the next Better Auth request with that session cookie. Every guard and every `getSession()` call is such a request.

```ts [server/auth.ts]
import { checkGitHubOrgMember, defineAuth } from 'vite-hub/auth'

const members = ['ada@example.com', 'grace@example.com']

export default defineAuth({
  access: {
    policy: {
      allow: async ({ user, accessToken }) => {
        if (user.emailVerified && members.includes(user.email)) return true
        const [error] = await checkGitHubOrgMember(await accessToken('github'), 'acme', { cacheMs: 300_000 })
        // During a GitHub outage, fail the request and keep the session.
        if (error?.code === 'AUTH_GITHUB_UNAVAILABLE') throw error
        return !error
      },
      recheck: 300_000,
    },
  },
})
```

| `allow` | Result |
| --- | --- |
| Returns `true` | The person signs in, or the session continues. Auth keeps the answer for `recheck` milliseconds. |
| Returns another value | Auth refuses the sign-in, or deletes the session and its cookies. The request continues signed out, so a guard answers `401`. |
| Throws | The request fails. The session stays, and Auth asks again on the next request. |

- `allow` gets the `user` and `accessToken(providerId)`, which returns the access token of the user's account with that provider, or `null`.
- Use [`checkGitHubOrgMember()`](https://vitehub.dev/docs/auth/server-api#github-organization-membership), which tells a refusal from a GitHub outage. `isGitHubOrgMember()` returns `false` for both, so a recheck while GitHub is down ends every session.
- `recheck` defaults to `300000`, five minutes. With `0`, Auth asks on each request. The answers are in memory, so each server process or Worker isolate keeps its own.
- [`verifyMcpRequest()`](https://vitehub.dev/docs/auth/configure#check-the-token-of-an-mcp-request) also asks `allow`, so an MCP client loses access with its person.
- The policy needs stored sessions: a Better Auth `database` or `secondaryStorage` adapter. With `stateless()`, Auth fails with `AUTH_C0066`.
- Define `allow` outside the Definition callback. A new function on each call builds a new Better Auth instance on each request.

## Typed user fields

The default client gets its types from the Auth Definition. When the Definition adds user or session fields, with `additionalFields`, with a plugin such as `admin()`, or with the session that `customSession()` returns, `useUserSession()` has these fields too.

```ts [server/auth.ts]
import { admin } from 'better-auth/plugins'
import { defineAuth } from 'vite-hub/auth'

export default defineAuth({
  plugins: [admin()],
  user: {
    additionalFields: {
      plan: { type: ['free', 'pro'], required: true, input: false },
    },
  },
})
```

```ts [lib/plan.ts]
import { useUserSession } from 'vite-hub/auth/vue'

const { user } = useUserSession()

export const isPro = () => user.value?.plan === 'pro'
export const isAdmin = () => user.value?.role === 'admin'
```

- ViteHub writes the type of the Definition to `.vitehub/types/auth.d.ts`. The client imports only this type, so no server code goes to the browser.
- In Nuxt, the auto-imported `useUserSession()` has the same types.
- A client from `createAuthClient()` does not get these fields. Add the Better Auth client plugins, such as `adminClient()` and `customSessionClient<RegisteredAuth>()`, and `inferAdditionalFields<RegisteredAuth>()` from `better-auth/client/plugins`. `RegisteredAuth` is a type from `vite-hub/auth`.

<!-- 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) (this page)
- [Hosts](https://vitehub.dev/raw/docs/auth/hosts.md)
- [Limits and errors](https://vitehub.dev/raw/docs/auth/limits-and-errors.md)
- [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: [AUTH_MCP_CLEANUP_FAILED](https://vitehub.dev/raw/docs/auth/diagnostics/auth_mcp_cleanup_failed.md)

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