JavaScript SDK

Next.js quickstart

Add Scute sign-in to a Next.js App Router app with the route handler, a client provider and ScuteAuthGate.

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@next

Set the app ID#

.env.localbash
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.

  1. 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.tsts
    import { cookies, headers } from "next/headers";import { ScuteHandler } from "@scute/nextjs-handlers";const handler = ScuteHandler({ cookies, headers });export { handler as GET, handler as POST };
  2. 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>  );}
  3. Wrap the root layout

    src/app/layout.tsxtsx
    import { 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).

  4. Read the user on the server

    src/app/account/page.tsxtsx
    import { 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.

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.

Next steps#

Server helpers
Route handlers, server actions, middleware, Edge.
Permission checks
Check a permission before an action.
Sign-in component
Theme and configure ScuteSignIn.