This is the setup npx create-scute-app writes for Next.js. It works on Next.js 13 to 16. The Pages Router setup is on Next.js.
Install#
npm install @scute/nextjs-handlers@next @scute/react-hooks@next @scute/auth-ui-react@nextSet the app ID#
NEXT_PUBLIC_SCUTE_APP_ID=YOUR_APP_ID@scute/nextjs-handlers reads NEXT_PUBLIC_SCUTE_APP_ID, NEXT_PUBLIC_SCUTE_BASE_URL (default https://api.scute.io) and, on the server only, SCUTE_SECRET. The secret is needed only for server calls such as permission checks.
Add the route handler
The browser client calls four endpoints under /auth: GET /auth/csrf, POST /auth/sign-in, POST /auth/refresh and POST /auth/sign-out. They move the refresh token into an httpOnly cookie, refresh the session and clear it on sign-out.
src/app/auth/[...scute]/route.tstsimport { cookies, headers } from "next/headers";import { ScuteHandler } from "@scute/nextjs-handlers";const handler = ScuteHandler({ cookies, headers });export { handler as GET, handler as POST };Create the client provider
createClientComponentClient keeps the session in cookies so server code can read it. Create it once per page with useState.
src/app/scute-providers.tsxtsx"use client";import { useState } from "react";import { createClientComponentClient } from "@scute/nextjs-handlers";import { AuthContextProvider } from "@scute/react-hooks";import { ScuteAuthGate } from "@scute/auth-ui-react";export function ScuteProviders({ children }: { children: React.ReactNode }) { // Reads NEXT_PUBLIC_SCUTE_APP_ID itself. const [client] = useState(() => createClientComponentClient()); return ( <AuthContextProvider scuteClient={client}> <ScuteAuthGate>{children}</ScuteAuthGate> </AuthContextProvider> );}Wrap the root layout
src/app/layout.tsxtsximport { ScuteProviders } from "./scute-providers";export default function RootLayout({ children }: { children: React.ReactNode }) { return ( <html lang="en"> <body> <ScuteProviders>{children}</ScuteProviders> </body> </html> );}ScuteAuthGate shows ScuteSignIn until the visitor is signed in, then renders the children. To keep some pages public, drop the gate from the provider and render ScuteSignIn on a sign-in page instead (see Sign-in component).
Read the user on the server
src/app/account/page.tsxtsximport { cookies } from "next/headers";import { createServerComponentClient } from "@scute/nextjs-handlers";export default async function AccountPage() { const cookieStore = await cookies(); const scute = createServerComponentClient({ cookies: () => cookieStore }); const { data, error } = await scute.getUser(); if (error || !data?.user) return <p>Signed out.</p>; return <p>Signed in as {data.user.email}</p>;}A server component client never refreshes a token (server components cannot set cookies); the browser client does that.
Magic links#
Email links land on the app's callback URL, set on the app's page in the dashboard. Render ScuteAuthGate or ScuteSignIn on that page: it reads sct_magic (or sct_oauth after Google or SAML) from the address and finishes the sign-in.