Quick Start
Install the package, set your keys, mount the provider, and protect loaders with Wacht.
Use this guide to add Wacht to a React Router app with hosted authentication.
Install the React Router package and its core dependencies.
pnpm add @wacht/react-router @wacht/jsx @wacht/types react-routerAdd the following keys to your environment file:
VITE_WACHT_PUBLISHABLE_KEY=pk_test_xxx
WACHT_API_KEY=wk_live_xxxDeploymentProvider gives your app access to Wacht's deployment-backed client context.
import { BrowserRouter } from 'react-router';
import { DeploymentProvider } from '@wacht/react-router';
export function App() {
return (
<DeploymentProvider publicKey={import.meta.env.VITE_WACHT_PUBLISHABLE_KEY}>
<BrowserRouter>{/* routes */}</BrowserRouter>
</DeploymentProvider>
);
}If you want the embedded auth UI to use app-specific UI settings, add uiOverwrites here.
Use authenticateRequest() in loaders or actions when a route requires an authenticated session.
import { json, redirect, type LoaderFunctionArgs } from 'react-router';
import { authenticateRequest } from '@wacht/react-router/server';
export async function loader({ request }: LoaderFunctionArgs) {
const result = await authenticateRequest(request);
if (!result.auth.isAuthenticated) {
throw redirect('/sign-in', { headers: result.headers });
}
return json({ userId: result.auth.userId }, { headers: result.headers });
}Forward the returned headers on every response path. That keeps session state synchronized correctly.
Use hosted auth pages first, then embed more of the auth surface only when you need it.
import {
NavigateToSignIn,
SignedIn,
SignedOut,
UserButton,
} from '@wacht/react-router';
export function AuthControls() {
return (
<>
<SignedIn>
<UserButton showName={false} />
</SignedIn>
<SignedOut>
<NavigateToSignIn />
</SignedOut>
</>
);
}Start the app locally:
pnpm devVisit the app, select Sign in, and complete the hosted authentication flow.
Use Integration model to understand the deployment-backed setup, then Authentication and Client-side Auth for the client-side model. Finish with Server Auth for the request helper surface.