JavaScript SDK

React quickstart

Add Scute sign-in to a React app with AuthContextProvider and ScuteAuthGate.

This setup fits any client-rendered React app (Vite and others). Remix, TanStack Start and Astro use the same provider; see Other frameworks.

Install#

npm install @scute/react-hooks@next @scute/auth-ui-react@next
.envbash
VITE_SCUTE_APP_ID=YOUR_APP_ID

The SDK does not read this variable itself: your code passes it to createClient.

  1. Create the provider

    src/scute-providers.tsxtsx
    import { useState, type ReactNode } from "react";import { AuthContextProvider, createClient } from "@scute/react-hooks";import { ScuteAuthGate } from "@scute/auth-ui-react";export function ScuteProviders({ children }: { children: ReactNode }) {  const [scute] = useState(() => createClient({ appId: import.meta.env.VITE_SCUTE_APP_ID }));  return (    <AuthContextProvider scuteClient={scute}>      <ScuteAuthGate>{children}</ScuteAuthGate>    </AuthContextProvider>  );}

    Create one client per app ID. A second client for the same app ID in one browser logs a warning, since the two can disagree about the session.

  2. Wrap the app

    src/main.tsxtsx
    import { createRoot } from "react-dom/client";import { ScuteProviders } from "./scute-providers";import { App } from "./App";createRoot(document.getElementById("root")!).render(  <ScuteProviders>    <App />  </ScuteProviders>);
  3. Use the session

    src/App.tsxtsx
    import { useAuth } from "@scute/react-hooks";export function App() {  const { user, signOut } = useAuth();  return (    <main>      <p>Signed in as {user?.email ?? user?.phone}</p>      <button onClick={() => signOut()}>Sign out</button>    </main>  );}

Session storage#

A browser client keeps the access and refresh tokens in localStorage, under keys suffixed with the app ID, and syncs sign-in and sign-out across tabs. To keep the refresh token out of JavaScript, use Next.js, which moves it into an httpOnly cookie, or pass your own storage adapter (see Client setup).

Your server#

Send the access token to your API and check it there. scute.getAuthToken() returns the current access token, refreshed first when it is about to expire.

const { data } = await scute.getAuthToken();await fetch("/api/orders", { headers: { Authorization: `Bearer ${data?.access}` } });

On the server, see Authenticate requests.

Next steps#

React hooks
useAuth, useScuteClient and the provider.
Headless sign-in
Draw every step yourself with useScuteAuthFlow.
MFA hooks
Enrollment and factor management.