new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Better Auth · all subjects

client apis/next.js

14 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Next.js API route handler setup with toNextJsHandler

Create an API route at `/api/auth/[...all]/route.ts` for the app router. Import the auth instance and use `toNextJsHandler` to mount the handler: `export const { GET, POST } = toNextJsHandler(auth);`

Next.js pages router API route handler setup with toNodeHandler

For pages router, create a route at `pages/api/auth/[...all].ts` using `toNodeHandler` instead of `toNextJsHandler`. Set `export const config = { api: { bodyParser: false } }` to disable body parsing, then export `toNodeHandler(auth.handler)` as the default export.

Next.js client creation with createAuthClient

Import `createAuthClient` from 'better-auth/react' and create a client instance with `export const authClient = createAuthClient({ //client configuration })`. The client uses nano-store for state management and re-renders components when state changes.

Accessing auth actions in Next.js server components and server actions

All actions from the auth instance can be invoked as functions through the `auth.api` object. To get the session in a server action or RSC, call `auth.api.getSession({ headers: await headers() })` after importing headers from 'next/headers'. Every endpoint made inside Better Auth is invocable as a function, including plugin endpoints.

Next.js 16+ proxy for auth protection with full validation

Create a `proxy.ts` file with `export async function proxy(request: NextRequest)`. Use `auth.api.getSession({ headers: await headers() })` for full session validation with database checks. Configure with `export const config = { matcher: ['/dashboard'] }` to specify routes. Node.js runtime is supported by default in Next.js 16+.

Next.js 16+ proxy cookie-only checks with getSessionCookie

For faster but less secure checks in Next.js 16+ proxy, use `getSessionCookie(request)` from 'better-auth/cookies'. This only checks for existence of a session cookie without validation. It is not secure and should not be relied upon for protection; use for optimistic redirects only.

Next.js 15.2.0+ Node.js runtime middleware for auth protection

In middleware.ts, set `export const config = { runtime: 'nodejs', matcher: ['/dashboard'] }`. Use `auth.api.getSession({ headers: await headers() })` for full validation. This is experimental in Next.js versions before 16.

Next.js 13-15.1.x edge runtime middleware session fetch

Middleware in older Next.js versions runs on Edge Runtime and cannot make database calls. To fetch the full session object, make an HTTP request to `/api/auth/get-session` route using a fetch library. Forward cookies from the request header: `cookie: request.headers.get('cookie') || ''`.

getSessionCookie custom cookie name and prefix configuration

When calling `getSessionCookie(request)`, pass a second argument with `cookieName` and `cookiePrefix` options if using custom values: `getSessionCookie(request, { cookieName: 'my_session_cookie', cookiePrefix: 'my_prefix' })`.

getCookieCache helper for session from cookie cache

Use `getCookieCache(request)` from 'better-auth/cookies' in middleware to get the session object from the cookie cache: `const session = await getCookieCache(request)`.

Page-level auth checks in Next.js server components

For page-level protection, check the session in each server component and use `redirect('/sign-in')` from 'next/navigation' if not authenticated. Example: get session with `auth.api.getSession({ headers: await headers() })`, then conditionally redirect.

Next.js 16 middleware to proxy migration

To migrate from middleware to proxy in Next.js 16, rename `middleware.ts` to `proxy.ts` and rename the `middleware` function to `proxy`. All Better Auth methods work identically. A codemod is available: `npx @next/codemod@canary middleware-to-proxy .`

RSC cookie cache refresh limitation

React Server Components cannot set cookies, so the cookie cache will not be refreshed until the server is interacted with from the client via Server Actions or Route Handlers.

getSessionCookie configuration must match auth.ts settings

When calling `getSessionCookie()`, if the auth configuration has customized cookie names or prefixes, the configuration passed to `getSessionCookie()` must match the values defined in `auth.ts`. The function does not automatically reference the auth config.

Give your agent this brain