Each helper returns a ScuteClient whose storage is the request's cookies. Call getUser() for the signed-in user, getSession() for the session, and admin methods for calls with SCUTE_SECRET.
| Helper | Where | Writes cookies | Refreshes |
|---|---|---|---|
| createServerComponentClient({ cookies }) | Server components, layouts, pages | No | No |
| createRouteHandlerClient({ cookies }) | App Router route handlers | Yes | Yes |
| createServerActionClient({ cookies }) | Server actions (same as the route handler client) | Yes | Yes |
| createMiddlewareClient({ req, res }) | Middleware | Yes | Yes |
| createPagesServerClient(context) | getServerSideProps, Pages API routes ({ req, res }) | Yes | Yes |
| createPagesEdgeRuntimeClient({ request, response? }) | Pages API routes on Edge | Yes, to response | Yes |
Each takes the client config as a second argument.
Server components#
import { cookies } from "next/headers";import { redirect } from "next/navigation";import { createServerComponentClient } from "@scute/nextjs-handlers";export default async function Dashboard() { const cookieStore = await cookies(); const scute = createServerComponentClient({ cookies: () => cookieStore }); const { data, error } = await scute.getUser(); if (error || !data?.user) redirect("/sign-in"); return <h1>Hello {data.user.email}</h1>;}Server components cannot set cookies, so this client never refreshes. An expired access token reads as signed out until the browser client refreshes it, or until middleware does.
Route handlers and server actions#
// app/api/projects/[id]/route.tsimport { cookies } from "next/headers";import { NextResponse } from "next/server";import { createRouteHandlerClient } from "@scute/nextjs-handlers";export async function PATCH(request: Request) { const scute = createRouteHandlerClient({ cookies }); const { data } = await scute.getUser(); if (!data?.user) return NextResponse.json({ error: "Sign in first." }, { status: 401 }); const { data: decision } = await scute.admin.authzCheck({ userId: data.user.id, action: "edit", resource: "project" }); if (!decision?.allowed) return NextResponse.json({ error: "Not allowed." }, { status: 403 }); // ...}createServerActionClient is the same function for "use server" actions.
Middleware#
// middleware.ts (Next.js 16: proxy.ts, exporting `proxy`)import { NextResponse, type NextRequest } from "next/server";import { createMiddlewareClient } from "@scute/nextjs-handlers";export async function middleware(req: NextRequest) { const res = NextResponse.next(); const scute = createMiddlewareClient({ req, res }); const { data } = await scute.getSession(); if (!data.session || data.session.status !== "authenticated") { return NextResponse.redirect(new URL("/sign-in", req.url)); } return res;}export const config = { matcher: ["/dashboard/:path*"] };Return the same res you passed in. Refreshed cookies are written to it as Set-Cookie for the browser and forwarded to the server components of the same request through Next's request override headers; cookie values are never put in a cookie response header.
Pages Router#
import type { GetServerSideProps } from "next";import { createPagesServerClient } from "@scute/nextjs-handlers";export const getServerSideProps: GetServerSideProps = async (context) => { const scute = createPagesServerClient(context); const { data } = await scute.getUser(); if (!data?.user) return { redirect: { destination: "/sign-in", permanent: false } }; return { props: { email: data.user.email } };};On Edge, pass the response you return: createPagesEdgeRuntimeClient({ request, response }). Without response, cookie writes reach only request.cookies for the rest of the request, never the browser.
Client IP#
Calls made with the secret key come from your server. The helpers send the signing-in person's address as X-Scute-Client-IP so the workspace's IP rules apply:
| Helper | Address sent by default |
|---|---|
| createPagesServerClient | req.socket.remoteAddress |
| createMiddlewareClient, createPagesEdgeRuntimeClient | request.ip, where Next.js provides it (Next.js 14 and earlier, on platforms that set it) |
| createRouteHandlerClient, createServerComponentClient, createServerActionClient | None |
| ScuteHandler (App Router) | request.ip where provided |
The helpers never read X-Forwarded-For or X-Real-IP. Behind a proxy or platform that reports the address, pass it yourself:
import { cookies, headers } from "next/headers";import { createRouteHandlerClient } from "@scute/nextjs-handlers";export async function POST() { // Only a header your platform sets and the browser cannot, e.g. x-real-ip on Vercel. const clientIp = (await headers()).get("x-real-ip"); const scute = createRouteHandlerClient({ cookies }, { clientIp }); // ...}clientIp wins over the detected address, and clientIp: null sends none. It must be one address; a list or a range throws. For one call, scute.admin.withClientIp(ip) returns a copy that sends ip.