createAuthHooks
createAuthHooks is a factory function that creates React hooks for managing Internet Identity authentication. The hooks are bound to a specific AuthenticationManager instance.
Import
Section titled “Import”import { AuthenticationManager, createAuthHooks } from "@ic-reactor/react"import { createAuthHooks } from "@ic-reactor/react"import { authentication } from "./reactor"
const { useAuth, useUserPrincipal, useAgentState } = createAuthHooks(authentication)Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
authentication |
AuthenticationManager |
The AuthenticationManager instance to bind to |
AuthenticationManager is constructed from a ClientManager:
const authentication = new AuthenticationManager({ clientManager })Pass authClient to inject a client you built yourself — IC Reactor then never
constructs one:
import { AuthClient } from "@icp-sdk/auth/client"
const authentication = new AuthenticationManager({ clientManager, authClient: new AuthClient(),})Return Value
Section titled “Return Value”createAuthHooks returns exactly three hooks:
| 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 { AuthenticationManager } from "@ic-reactor/react"import { QueryClient } from "@tanstack/query-core"
export const queryClient = new QueryClient()
export const clientManager = new ClientManager({ queryClient,})
export const authentication = new AuthenticationManager({ clientManager })import { createAuthHooks } from "@ic-reactor/react"import { authentication } from "./index"
export const { useAuth, useUserPrincipal, useAgentState } = createAuthHooks(authentication)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