Client hook binding a TokenManager instance to the React session
Client-side helpers for working with the WooCommerce session token in React. useTokenManager lazily instantiates the app's concrete TokenManager and returns the singleton instance; the companion exports expose the readiness flag and the instance outside React.
All three are exported from the client entry point, @woographql/next.
import { useTokenManager, getTokenManagerInstance, useSessionFlag } from '@woographql/next'; import type { Cookies } from '@woographql/next';
| Export | Signature | Description |
|---|---|---|
useTokenManager | <T extends TokenManager>(TokenManagerClass: new () => T) => T | Construct (once) and return the app's TokenManager instance, memoized for the component tree. |
getTokenManagerInstance | <T extends TokenManager>() => T | null | Imperative accessor for the singleton instance outside React (e.g. in a server action helper). Returns null before the first useTokenManager call. |
useSessionFlag | () => string | null | Track the SESSION_FLAG cookie value, returning null until the first client-side read settles (so SSR markup matches the first client paint). Used as the session store's revalidate key so it re-fetches when server-side auth state changes. |
Cookies (type) | { name: string; value: string }[] | Shape of the cookie bag a TokenManager reads/writes. |
useSessionFlag pairs a cookieStore change listener with a polling fallback, because Chromium's change events for HTTP-set cookies (server-action responses, Set-Cookie, etc.) are inconsistent — so login/logout writes are still caught.
'use client'; import { useTokenManager } from '@woographql/next'; import { ClientTokenManager } from '@auth/ClientTokenManager'; export function useAuthHeader() { const tokenManager = useTokenManager(ClientTokenManager); // tokenManager.getTokens(), tokenManager.saveTokens(...), etc. return tokenManager; }
Application code typically imports the bound hook from @auth/SessionProvider rather than calling useTokenManager directly — the generated provider passes the right concrete class for your credentialStorageType.
SESSION_FLAG is stamped server-side