createAuthHooks
createAuthHooks is a factory function that creates React hooks for managing Internet Identity authentication. The hooks are bound to a specific ClientManager instance.
Import
Section titled “Import”import { createAuthHooks } from "@ic-reactor/react"import { createAuthHooks } from "@ic-reactor/react"import { clientManager } from "./reactor"
const { useAuth, useUserPrincipal, useAgentState } = createAuthHooks(clientManager)Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
clientManager | ClientManager | The ClientManager instance to bind to |
Return Value
Section titled “Return Value”createAuthHooks returns an object containing:
| Property | Type | Description |
|---|---|---|
useAuth | Hook | Main hook for auth actions and state |
useUserPrincipal | Hook | Current user’s Principal |
useAgentState | Hook | Agent initialization state |
Complete Example
Section titled “Complete Example”import { ClientManager } from "@ic-reactor/core"import { QueryClient } from "@tanstack/query-core"
export const queryClient = new QueryClient()
export const clientManager = new ClientManager({ queryClient, withProcessEnv: true,})import { createAuthHooks } from "@ic-reactor/react"import { clientManager } from "./index"
export const { useAuth, useUserPrincipal, useAgentState } = createAuthHooks(clientManager)import { useAgentState } from "./reactor/hooks"import { QueryClientProvider } from "@tanstack/react-query"import { queryClient } from "./reactor"
function App() { const { isInitializing, error } = useAgentState()
if (isInitializing) return <LoadingScreen /> if (error) return <ErrorScreen error={error} />
return ( <QueryClientProvider client={queryClient}> <Router /> </QueryClientProvider> )}See Also
Section titled “See Also”- Authentication Guide — In-depth auth patterns
- useAuth — Main auth hook reference
- createActorHooks — Creating actor hooks
- React Setup — Complete React configuration