Skip to Content

React

React hooks and the provider that read a client's state.

Import these from @monarcode/session-kit/react. To set it up, see Read the session in React.

Functions

createAuthHooks

Creates React hooks typed for one auth client. Use it when an app has several clients: each call has its own context, so call it once per client. With one client, register it and use useAuth, useAuthClient and AuthProvider instead.

See AuthHooksOptions to find the client without AuthProvider.

useAuth

Reads the current auth state, or the part selector picks, and re-renders when it changes. With a selector, it re-renders only when that part changes.

The part is compared with Object.is. A selector that returns a new object or array each time re-renders on every change.

It reads the client of the nearest AuthProvider, typed by the client you register in Register, from @monarcode/session-kit.

Throws INVALID_OPTIONS without an AuthProvider above. With TanStack Router, use the hooks from @monarcode/session-kit/tanstack-router instead: they read Router context, and need no AuthProvider.

useAuthClient

Returns the client of the nearest AuthProvider, for actions such as signIn and signOut. It's typed by the client you register in Register, from @monarcode/session-kit (ProvidedAuth).

Throws INVALID_OPTIONS without an AuthProvider above, as useAuth does.

useMirrorAuth

Creates a createMirrorAuth client once, from the session the page was rendered with, and keeps it in step with your server. Call it in the component that renders AuthProvider.

When a new server render brings another session, such as after a server action signs in or out, the client takes it and tells other tabs. When the session changes in the browser, it calls onChange.

Throws INVALID_OPTIONS without a create or an onChange function.

Creating the client here, rather than at module level, keeps one per request during server rendering.

See UseMirrorAuthOptions for create, session and onChange.

Components

AuthProvider

Gives a client to the hooks below it, and mounts it while rendered, which starts expiry timers and following other tabs. Render it once, around your app.

Mounting restores the saved session, before the first paint with synchronous storage and schemas. On a server, nothing starts, as effects don't run there.

Its client is typed by the client you register in Register, from @monarcode/session-kit. Until you register one, it takes any client (ProvidedAuth).

See useAuth to read the session below it.

Types

AuthHooks

The provider and hooks createAuthHooks returns, typed for one client.

AuthHooksOptions

Options for createAuthHooks.

Prop

Type

AuthProviderProps

Props for AuthProvider, and for the one that createAuthHooks returns.

Prop

Type

AuthSource

What the React bindings need from an auth client: its state, and mount to start background work while rendered. A createAuth or createMirrorAuth client fits.

ProvidedAuth

The client AuthProvider takes and useAuthClient returns: the one you register in Register, from @monarcode/session-kit.

Until you register one, it's any client, with what every client has: its state, mount, getSession, signOut, isCurrent and credentials. Its user is then unknown.

StateOf

The state type of a client: what useAuth() returns (AuthState).

UseMirrorAuthOptions

Options for useMirrorAuth.

Prop

Type

On this page