React hooks and components for client-side authentication.
The SDK provides React hooks for managing authentication state in client-side applications. Import from @warpy-auth-sdk/core/hooks.
Context provider for managing session state in React apps
AuthProvider(props: AuthProviderProps): ReactElementchildrenReactNoderequiredChild components
secretstringrequiredJWT signing secret (not used client-side, for compatibility)
onSignIn(session: Session) => voidCallback when user signs in
onSignOut() => voidCallback when user signs out
ReactElementimport { AuthProvider } from '@warpy-auth-sdk/core/hooks';
function App() {
return (
<AuthProvider
secret={process.env.NEXT_PUBLIC_AUTH_SECRET!}
onSignIn={(session) => {
console.log('User signed in:', session.user);
}}
onSignOut={() => {
console.log('User signed out');
}}
>
<YourApp />
</AuthProvider>
);
}Hook for accessing session state and auth methods
useAuth(): AuthContextValueAuthContextValueimport { useAuth } from '@warpy-auth-sdk/core/hooks';
function ProfilePage() {
const { session, loading, signIn, signOut, refreshSession } = useAuth();
if (loading) return <div>Loading...</div>;
if (!session) {
return (
<div>
<h1>Sign In</h1>
<button onClick={() => signIn('user@example.com')}>
Sign in with Email
</button>
</div>
);
}
return (
<div>
<h1>Welcome, {session.user.name}!</h1>
<p>Email: {session.user.email}</p>
<button onClick={signOut}>Sign Out</button>
</div>
);
}interface AuthContextValue {
// Current session (null if not authenticated)
session: Session | null;
// Loading state (true during initial session fetch)
loading: boolean;
// Sign in with email (sends magic link)
signIn: (email: string, captchaToken?: string) => Promise<void>;
// Sign out current user
signOut: () => Promise<void>;
// Manually refresh session from server
refreshSession: () => Promise<void>;
}Server-side helper for getting session in React Server Components. Import from @warpy-auth-sdk/core/hooks/server.
Get session in React Server Components or API routes
getServerSession(request: Request, secret: string): Promise<Session | null>requestRequestrequiredHTTP request object with cookies
secretstringrequiredJWT signing secret
Promise<Session | null>import { getServerSession } from '@warpy-auth-sdk/core/hooks/server';
import { cookies } from 'next/headers';
export default async function ServerPage() {
// In Next.js App Router
const cookieStore = await cookies();
const cookieHeader = cookieStore.toString();
const request = new Request('http://localhost', {
headers: { cookie: cookieHeader },
});
const session = await getServerSession(request, process.env.AUTH_SECRET!);
if (!session) {
return <div>Not authenticated</div>;
}
return (
<div>
<h1>Server Component</h1>
<p>User: {session.user.email}</p>
</div>
);
}interface Session {
user: {
id: string;
email: string;
name?: string;
picture?: string;
};
expires: Date;
token?: string; // JWT token (for MCP agents)
type?: "standard" | "mcp-agent";
scopes?: string[]; // For MCP agents
agentId?: string; // For MCP agents
}The AuthProvider automatically:
/api/auth/session on mountUse the correct import for your environment:
@warpy-auth-sdk/core/hooks - Client components (useAuth, AuthProvider)@warpy-auth-sdk/core/hooks/server - Server components (getServerSession)useAuth in React Server ComponentsgetServerSession in client componentsThe React hooks expect the following API endpoints to be available:
// Session endpoint
GET /api/auth/session
Response: { session: Session | null }
// Sign in endpoint (email)
POST /api/auth/signin/email
Body: { email: string, captchaToken?: string }
Response: { success: true } or { error: string }
// Sign out endpoint
POST /api/auth/signout
Response: { success: true }
// OAuth endpoints
GET /api/auth/signin/:provider // Redirects to OAuth provider
GET /api/auth/callback/:provider // OAuth callback (sets session cookie)These endpoints are automatically created when using framework adapters or Next.js middleware.