@scute/nextjs-handlers supports Next.js 13 to 16, the App and Pages Router, on the Node and Edge runtimes. The session lives in cookies so server code can read it, and the refresh token stays in an httpOnly cookie the browser's JavaScript cannot read. It re-exports everything in @scute/js-core.
Environment#
| Variable | Read by | |
|---|---|---|
| NEXT_PUBLIC_SCUTE_APP_ID | Every client | Required unless you pass appId. Missing both throws ScuteError. |
| NEXT_PUBLIC_SCUTE_BASE_URL | Every client | Optional. Default https://api.scute.io. |
| SCUTE_SECRET | Server clients only | Optional. Sent on admin calls, such as permission checks. Never read in the browser. |
Every client takes a config (ScuteNextjsClientConfig): the ScuteClient options with appId optional, persistSession always on, and one extra preference, httpOnlyRefresh (default true). All of its requests to Scute are sent with cache: "no-store".
How the session moves#
- The browser client signs in with Scute and gets a token payload.
- With httpOnlyRefresh on, it posts the access token to /auth/sign-in with a CSRF token instead of storing the refresh token itself.
- The handler exchanges that access token (admin.refreshWithAccess) and sets the session cookies, the refresh token httpOnly.
- To refresh, the browser posts to /auth/refresh; the handler refreshes with the cookie and returns only the new access token.
- On signed_out or session_expired, the browser posts to /auth/sign-out, which revokes the session and clears the cookies.
The route handler#
// app/auth/[...scute]/route.tsimport { cookies, headers } from "next/headers";import { ScuteHandler } from "@scute/nextjs-handlers";const handler = ScuteHandler({ cookies, headers });export { handler as GET, handler as POST };ScuteHandler picks its mode from its arguments: (context, config?) for the App Router, (req, res, config?) for Pages API routes on Node, and (req, config?) for Pages API routes on Edge.
| Endpoint | Does |
|---|---|
| GET /auth/csrf | Returns the CSRF token and sets it in an httpOnly cookie X-CSRF-Token__<appId>. Sent with Cache-Control: no-store. |
| POST /auth/sign-in | Needs the CSRF header and Authorization: Bearer <access token>. Accepts only an access token issued within the last 30 seconds, clears this app's existing session cookies, then exchanges exactly that token. 401 otherwise. |
| POST /auth/refresh | Needs the CSRF header. Returns { access, access_expires_at }. A refused refresh clears the session and answers 401; a 5xx or network failure keeps the cookies and answers 502 or 503; a blocked network answers 403 with error_code ip_blocked or country_blocked and keeps the cookies. |
| POST /auth/sign-out | Needs the CSRF header. Revokes the session and deletes the cookies. |
Any other path or method answers 400 without calling Scute.
CSRF#
The browser sends the token from /auth/csrf back in the X-CSRF-Token header; the handler compares it with the cookie in constant time. A mismatch answers 401 CSRF error. The cookie name carries the app ID, so two Scute apps on one host keep separate tokens.
fetchWithCsrf(handler, init?, prefix?) is exported: it fetches the token, then calls the handler with it. It never sends the request without a token; when the CSRF endpoint fails it returns a non-ok Response.
The browser client#
"use client";import { useState } from "react";import { createClientComponentClient } from "@scute/nextjs-handlers";import { AuthContextProvider } from "@scute/react-hooks";export function Providers({ children }: { children: React.ReactNode }) { const [client] = useState(() => createClientComponentClient()); return <AuthContextProvider scuteClient={client}>{children}</AuthContextProvider>;}createClientComponentClient(config?) stores the session in browser cookies (ScuteBrowserCookieStorage, Secure in production) and returns the same client on every call in the browser.
| Option | |
|---|---|
| handlersPrefix | Where the handler lives when it is not at /auth. With "app", the client calls /app/auth/.... |
| preferences.httpOnlyRefresh | false keeps the refresh token in a browser cookie and skips the handler. |
For the Pages Router, createPagesBrowserClient(config?) is the same client with handlersPrefix starting with api (/api/auth/...).
Server code#
Server components, route handlers, server actions, middleware and Edge each have their own client. See Server helpers.