React SDK
@authon/react — React components and hooks for Authon. Works with Create React App, Vite, and any React 18+ project.
Installation
npm install @authon/reactAuthonProvider
Wrap your app root with AuthonProvider. All hooks and components must be rendered inside this provider.
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { AuthonProvider } from "@authon/react";
import App from "./App";
createRoot(document.getElementById("root")!).render(
<StrictMode>
<AuthonProvider publishableKey="pk_live_your_publishable_key">
<App />
</AuthonProvider>
</StrictMode>
);Provider Props
| Prop | Type | Description |
|---|---|---|
| publishableKey | string | Your pk_live_ or pk_test_ key from the dashboard |
| config.apiUrl | string? | Override the Authon API base URL |
| config.theme | 'light' | 'dark' | 'auto' | Color theme for the modal (default: auto) |
| config.locale | string? | Locale string for the modal UI (default: en) |
| config.appearance | Partial<BrandingConfig>? | Override branding config programmatically |
Hooks
useAuthon()
The primary hook for interacting with auth state. Returns the full context value including methods to open the modal, get the token, and sign out.
import { useAuthon } from "@authon/react";
function Header() {
const {
isSignedIn,
isLoading,
user,
openSignIn,
openSignUp,
signOut,
getToken,
} = useAuthon();
if (isLoading) return <Spinner />;
return (
<nav>
{isSignedIn ? (
<>
<span>Hello, {user?.displayName}</span>
<button onClick={() => signOut()}>Sign out</button>
</>
) : (
<button onClick={() => openSignIn()}>Sign in</button>
)}
</nav>
);
}useUser()
A focused hook that returns only user data and loading state. Use this in components that only need to display user information.
import { useUser } from "@authon/react";
function ProfileCard() {
const { user, isLoading } = useUser();
if (isLoading) return <Skeleton />;
if (!user) return null;
return (
<div className="profile-card">
{user.avatarUrl && (
<img src={user.avatarUrl} alt={user.displayName ?? ""} />
)}
<h2>{user.displayName}</h2>
<p>{user.email}</p>
<span>Member since {new Date(user.createdAt).getFullYear()}</span>
</div>
);
}Components
SignedIn / SignedOut
Conditionally render children based on authentication state. These are the easiest way to build auth-aware UIs without manual conditionals.
import { SignedIn, SignedOut } from "@authon/react";
function App() {
return (
<>
<SignedIn>
{/* Only rendered when user is signed in */}
<Dashboard />
<UserButton />
</SignedIn>
<SignedOut>
{/* Only rendered when user is NOT signed in */}
<LandingPage />
<SignInButton />
</SignedOut>
</>
);
}SignIn / SignUp
Trigger the Authon modal UI. In popup mode, the modal opens on mount. In embedded mode, the form renders inline.
import { SignIn, SignUp } from "@authon/react";
// Popup — opens the modal when mounted
function LoginPage() {
return <SignIn mode="popup" />;
}
// Embedded — renders a form container in-place
function SignUpPage() {
return (
<div className="flex min-h-screen items-center justify-center">
<SignUp mode="embedded" />
</div>
);
}UserButton
Drop-in avatar button with a sign-out dropdown. Shows the user's avatar if available, or falls back to initials with a gradient background. Returns null when not signed in.
import { UserButton } from "@authon/react";
function AppHeader() {
return (
<header className="flex items-center justify-between p-4">
<Logo />
<UserButton />
</header>
);
}Protected Routes
CombineuseAuthon()with React Router to guard private routes:
import { Navigate } from "react-router-dom";
import { useAuthon } from "@authon/react";
interface ProtectedRouteProps {
children: React.ReactNode;
redirectTo?: string;
}
export function ProtectedRoute({
children,
redirectTo = "/sign-in",
}: ProtectedRouteProps) {
const { isSignedIn, isLoading } = useAuthon();
if (isLoading) return <FullPageSpinner />;
if (!isSignedIn) return <Navigate to={redirectTo} replace />;
return <>{children}</>;
}
// Usage in router
const router = createBrowserRouter([
{ path: "/", element: <Home /> },
{
path: "/dashboard",
element: (
<ProtectedRoute>
<Dashboard />
</ProtectedRoute>
),
},
]);Using the Token
UsegetToken() to attach the JWT to your backend API calls:
import { useAuthon } from "@authon/react";
function useApi() {
const { getToken } = useAuthon();
const apiFetch = async (path: string, init?: RequestInit) => {
const token = getToken();
return fetch(`/api${path}`, {
...init,
headers: {
...init?.headers,
Authorization: token ? `Bearer ${token}` : "",
"Content-Type": "application/json",
},
});
};
return { apiFetch };
}
// In a component
function OrdersList() {
const { apiFetch } = useApi();
const [orders, setOrders] = useState([]);
useEffect(() => {
apiFetch("/orders").then((r) => r.json()).then(setOrders);
}, []);
return <ul>{orders.map((o) => <li key={o.id}>{o.name}</li>)}</ul>;
}