---
title: Frameworks
description: One core on the web Request, with optional adapters for TanStack Start and Next.js.
---

# Frameworks

The core of the package works on a web `Request` and gives back a `Response` or cookie headers. Nothing in it depends on a framework. There are two ways to call it:

| Way | Use it with | Calls look like |
| --- | --- | --- |
| An adapter (`framework` option) | TanStack Start, Next.js | `auth.user()`, `auth.password.signIn(data)` |
| `auth.withRequest(request)` | Hono, Bun.serve, SvelteKit, React Router, Astro, Elysia, … | `auth.withRequest(request).user()` |

Both give the same API, types and errors. The package does not guess the framework: you choose the adapter, or none.

In every framework, mount `auth.handler` (or `auth.routes`) on `<basePath>/*`, default `/auth/*`. Those routes handle the Google and GitHub redirects, e-mail links, sign-out and dev login.

## TanStack Start

```ts
import { tanstackStart } from "@kaaarel/auth/tanstack";

createAuth({ /* … */ framework: tanstackStart() });
```

The adapter reads the request and sets cookies through TanStack Start. It works with 1.131+ (`getWebRequest`) and 1.160+ (`getRequest`).

```ts
// TanStack Start 1.160+
export const Route = createFileRoute("/auth/$")({ server: { handlers: auth.routes } });

// TanStack Start 1.131
export const ServerRoute = createServerFileRoute("/auth/$").methods(auth.routes);
```

- Call auth functions only on the server: in server functions and server routes.
- In `beforeLoad`, call a server function that calls `auth.user()`. `beforeLoad` also runs in the browser.
- `requireUser()` and `workspaces.requireMember()` throw a TanStack `redirect` when signed out. Inside a server function, `next` comes from the page (the `Referer`), not from the server function URL.
- Make every server function that changes something `method: "POST"`. The package refuses changes over GET.

## Next.js (App Router)

```ts
import { nextjs } from "@kaaarel/auth/next";

createAuth({ /* … */ framework: nextjs() });
```

```ts
// app/auth/[...auth]/route.ts
export const GET = auth.handler;
export const POST = auth.handler;
```

The adapter builds the request from `headers()` and sets cookies with `cookies()`. It needs Next.js 15 or newer.

- **Server actions** change things: sign-in, sign-up, reset, settings. They are POST requests, so the package accepts them.
- **Server components** only read: `auth.user()`, `auth.session()`, `auth.requireUser({ next: "/app" })`. A server component cannot see its own path, so pass `next` yourself. A server component also cannot set cookies; a session refresh then waits for the next action or route handler.
- **Route handlers** can use the adapter too, or `auth.withRequest(request)`.
- `requireUser()` throws Next's `redirect` when signed out.

## Any other framework

Use `auth.withRequest(request)`. It binds the API to one request. Cookies collect in `headers`; copy them to your response.

```ts
async function signIn(request: Request) {
  const api = auth.withRequest(request);
  const result = await api.password.signIn(await request.json());
  return Response.json(result, { headers: api.headers });
}
```

```ts
// Bun.serve
Bun.serve({
  fetch(request) {
    if (new URL(request.url).pathname.startsWith("/auth/")) return auth.handler(request);
    // …
  },
});

// Hono
app.all("/auth/*", (c) => auth.handler(c.req.raw));

// SvelteKit: src/routes/auth/[...path]/+server.ts
export const GET = ({ request }) => auth.handler(request);
export const POST = ({ request }) => auth.handler(request);

// React Router / Remix: app/routes/auth.$.ts
export const loader = ({ request }) => auth.handler(request);
export const action = ({ request }) => auth.handler(request);
```

Without an adapter, `requireUser()` throws `AuthException("not_signed_in")` instead of redirecting. Catch it and redirect the way your framework does.

## Rules for every framework

- Changes over GET are refused (`forbidden_origin`). Use POST handlers, actions or server functions for them.
- Browser code imports only `@kaaarel/auth/errors` (types and messages). `@kaaarel/auth` is server code.
- The paths are yours: the config file can live anywhere, `pages` holds your page paths, and `basePath` moves `/auth/*`.

## Write an adapter

An adapter has three functions and one optional one:

```ts
import type { FrameworkAdapter } from "@kaaarel/auth";

const adapter: FrameworkAdapter = {
  getRequest: () => currentRequest(),                // may be async; return the same object within one request
  setCookie: (name, value, options) => setResponseCookie(name, value, options), // may be async
  redirect: (to) => { throw frameworkRedirect(to); },
  getIP: () => socketAddress(),                      // optional: used when no proxy header gives the client IP
};
```
