Auth provider
A live demo against a mocked server in your browser — the block is the registry's own source.
Install
Section titled “Install”npx shadcn add @mantle/auth-providerNeeds a React Aria shadcn project and the @mantle registry in components.json — see
Install. The block lands in components/mantle/auth-provider.tsx, yours to edit.
Wrap your app once, inside <MantleProvider>. Every auth block reads the session from it.
import { mantle } from "@mantlejs/client";import { MantleProvider } from "@mantlejs/react";import { AuthProvider, useAuth } from "@/components/mantle/auth-provider";
const client = mantle({ url: "https://api.example.com" });
export function App() { return ( <MantleProvider client={client}> <AuthProvider> <Header /> </AuthProvider> </MantleProvider> );}
function Header() { const { status, user, logout } = useAuth(); if (status === "loading") return null; return status === "authenticated" ? ( <button onClick={() => void logout()}>Log out</button> ) : ( <a href="/login">Log in</a> );}statusis"loading"until the persisted-session check on mount resolves;useris set bylogin()and isundefinedafter a reload (the client persists tokens, not the user record).- With
handleOAuthRedirect(default on), tokens arriving in the URL fragment from an@mantlejs/auth-oauthredirect are handed to the client and stripped from the address bar. signup()creates the user through theusersservice (configurable withusersService), then logs in.