---
title: "@xid-kit/nextjs"
description: "Next.js middleware, App Router and Pages Router server helpers, plus React SDK re-exports."
locale: "en"
---

> Documentation Index
> Fetch the locale documentation index at: https://xid.dev/en/llms.txt
> Use this file to discover all available pages before exploring further.

# @xid-kit/nextjs

## Middleware

`xidMiddleware()` runs on the Edge Runtime, verifies the session JWT networklessly, and injects auth state into downstream request headers for server components and route handlers.

```ts
import { xidMiddleware } from '@xid-kit/nextjs'

export default xidMiddleware({
  jwtKey: process.env.XID_JWKS_PUBLIC_KEY!,
  issuer: 'https://xid.dev',
})

export const config = {
  matcher: ['/dashboard(.*)', '/api/protected(.*)'],
}
```

## App Router server helpers

```ts
import { auth, currentUser } from '@xid-kit/nextjs'

export default async function DashboardPage() {
  const { userId } = await auth()
  if (!userId) return null
  const user = await currentUser()
  return <p>Welcome {user?.email}</p>
}
```

## Pages Router

```ts
import { getAuth } from '@xid-kit/nextjs'

export const getServerSideProps = async (ctx) => {
  const { userId } = await getAuth(ctx.req)
  if (!userId) return { redirect: { destination: '/sign-in', permanent: false } }
  return { props: {} }
}
```

## Exported API

| Export | Kind | Purpose |
| --- | --- | --- |
| `xidMiddleware` | function | Edge Runtime middleware: verifies JWT, injects auth headers |
| `auth` | function | App Router: returns AuthObject with userId, orgId, orgRole |
| `getAuth` | function | Pages Router: returns AuthObject from IncomingMessage |
| `currentUser` | function | App Router: fetches full user object using server auth context |
| `xidClient` | function | Returns server-side XidApiClient bound to current request auth |
| `XID_AUTH_HEADER` | string constant | Header name for auth state between middleware and server components |
| `XidMiddlewareOptions` | type | Options for xidMiddleware: jwtKey, issuer, publicRoutes, ignoredRoutes |
| `XidServerClientOptions` | type | Options for xidClient |
| `AuthObject` | type | Authenticated state: userId, orgId, orgRole, sessionId |
| `UnauthenticatedAuthObject` | type | Unauthenticated state with null fields |
| `AuthResult` | type | Union of AuthObject and UnauthenticatedAuthObject |
| `PaginationParams` | type | Cursor and limit params for Management API list calls |
| `PaginatedResponse<T>` | type | Paginated response envelope |

## Re-exports

- Re-exports all [@xid-kit/react](/sdks/react) client components and hooks via `export * from "@xid-kit/react"`.
- Re-exports from [@xid-kit/backend](/sdks/backend): `verifyToken`, `verifyWebhook`, `authenticateRequest`, `JwksCache`, `toVerifyKeySet`, `AppError`, `BACKEND_ERROR_CODES`, and all their associated types.
- Server helpers never expose signing secrets to client bundles.

Source: https://xid.dev/sdks/nextjs/index.mdx
