Skip to main content

Overview

@kavachos/react provides React hooks and a context provider for building auth UIs on top of KavachOS. It works with Next.js App Router, Next.js Pages Router, Vite, and any React 18+ setup. The package itself has no Node.js dependencies, it runs entirely in the browser. Your KavachOS API handler can sit behind Next.js Edge Runtime, Cloudflare Workers, Deno Deploy, or any other edge runtime, and the hooks talk to it over standard fetch.
All hooks must be rendered inside KavachProvider. The provider talks to your KavachOS API route, no direct database access from the browser.

Installation

Provider setup

Wrap your app with KavachProvider. In Next.js App Router, create a client component and import it from your root layout.
string
default:"\"/api/kavach\""
Path to your KavachOS API handler. Must match the route you mounted in the adapter.
RequestInit
default:"undefined"
Merged into every fetch call. Use this to add custom headers or credentials mode.
Sessions are stored in localStorage under the key kavach_session. The hooks automatically restore the session on page reload.

useSession

Returns the raw session object. Useful when you need the session token or expiry directly.
Session | null
The current session. Null when unauthenticated or still loading.
boolean
True during the initial session fetch.
Force a re-fetch of the session. Call this after making out-of-band auth changes.

useUser

Returns the authenticated user and a boolean flag. This is the most common hook for protecting UI.
User | null
The authenticated user object. Null when unauthenticated.
boolean
True when a valid session with a user is present.
boolean
True during the initial load. Avoid rendering auth-gated UI until this is false.

useSignIn

Handles email and password sign-in. Returns a signIn function and state fields. The hook posts to POST /api/kavach/auth/sign-in (not /sign-in/email). Make sure your adapter is mounted at /api/kavach.

useSignUp

Handles new account registration. Posts to POST /api/kavach/auth/sign-up (not /sign-up/email).

useSignOut

Signs the user out and clears the local session. Optionally redirects after sign-out.

useAgents

Lets your UI create, list, and revoke agents without going through a custom API route.
Agent[]
Current list of agents for the authenticated user.
Create a new agent. The list refreshes automatically on success.
Revoke an agent by ID. Removes it from the local list on success.
Re-fetch the agent list manually.
boolean
True during the initial agents fetch.

Next steps

TypeScript client

Server-side and Node.js usage with @kavachos/client.

Agent identity

How agents are modelled and what fields they carry.

Adapters

Mount the KavachOS handler in Next.js, Express, Hono, and others.
Last modified on April 29, 2026