# Next.js (https://session-kit.pages.dev/docs/reference/next)

```ts
import { … } from "@monarcode/session-kit/next";
```

Guide: [Next.js](https://session-kit.pages.dev/docs/setups/next).

## Functions

### nextJar

The `CookieJar` for `cookies()` from `next/headers`, to pass to `serverAuth` from `@monarcode/session-kit/server`.

### nextSession

Gives a Next.js app its session ([`NextSession`](#nextsession)), over [`nextJar`](#nextjar) and [`proxyJar`](#proxyjar). Make it once, beside `serverAuth`, in a module only server code imports.

### proxyJar

The `CookieJar` for a request in Next's Proxy (`proxy.ts`). It reads `request`'s cookies and sets new ones on `response`. Use it with `serverAuth.refreshIfDue`, so the pages that render the request see a fresh token through [`nextJar`](#nextjar).

### sessionRoute

Makes the Route Handlers for your session route, which answers `createMirrorAuth`'s `endpoint`: the browser client reads the session, signs out and gets the access token through it. Pass `cookies` from `next/headers` ([`NextSessionRouteOptions`](#nextsessionrouteoptions)).

## Types

### NextCookies

A request's cookie store, as `cookies()` from `next/headers` resolves it. [`nextJar`](#nextjar) reads and writes it, and Next's own store fits.

### NextJarOptions

Options for [`nextJar`](#nextjar).

### NextJarOptions

Options for nextJar.

| Prop        | Type    | Description                                                                                                                                                                                                |
| ----------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `writable?` | `union` | Whether the jar can set cookies. Pass `true` in a Server Function (an action) or a Route Handler. Server Components can't set cookies, so there the jar is read-only and never refreshes. Default: `false` |

### NextSession

The session in a Next.js app, made by [`nextSession`](#nextsession): reads and guards for Server Components, the request's session for Server Functions and Route Handlers, and the refresh for Proxy.

### NextSessionOptions

Options for [`nextSession`](#nextsession). Pass Next's own `cookies` and `redirect`, as this entry point doesn't import `next`.

### NextSessionOptions

Options for nextSession. Pass Next's own `cookies` and `redirect`,
as this entry point doesn't import `next`.

| Prop         | Type       | Description                                                                                                                       |
| ------------ | ---------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `cookies`    | `function` | `cookies` from `next/headers`.                                                                                                    |
| `redirect`   | `function` | `redirect` from `next/navigation`.                                                                                                |
| `loginPath?` | `string`   | Your sign-in page's path, starting with `/`. Default: `"/login"`                                                                  |
| `within?`    | `number`   | How soon before the access token expires `refresh` refreshes it, in seconds, as `serverAuth.refreshIfDue` explains. Default: `60` |

### NextSessionRoute

The Route Handlers [`sessionRoute`](#sessionroute) makes, for `GET` and `POST`.

### NextSessionRouteOptions

Options for [`sessionRoute`](#sessionroute). Pass Next's own `cookies`, as this entry point doesn't import `next`.

### NextSessionRouteOptions

Options for sessionRoute. Pass Next's own `cookies`, as this entry
point doesn't import `next`.

| Prop      | Type       | Description                    |
| --------- | ---------- | ------------------------------ |
| `cookies` | `function` | `cookies` from `next/headers`. |

### ProxyRequest

The request [`proxyJar`](#proxyjar) reads cookies from. A `NextRequest` fits.

### ProxyResponse

The response [`proxyJar`](#proxyjar) sets cookies on. A `NextResponse` fits.