Abstract base class managing the WooCommerce session-token lifecycle
TokenManager is the abstract base class that manages the WooCommerce session-token lifecycle — issuing, refreshing, persisting, and clearing the auth / session / refresh tokens. It extends AsyncTokenManager from @woographql/session-utils and leaves storage to the subclass: each generated app ships a concrete manager (client, server, and/or middleware) that implements the storage primitives against cookies or localStorage.
It is exported from both entry points — @woographql/next (client-safe) and @woographql/next/server — because both the browser and server managers extend it.
import { TokenManager } from '@woographql/next'; // or, server-side: import { TokenManager } from '@woographql/next/server';
A subclass implements the storage primitives; the base class provides the token lifecycle on top of them.
| Member | Signature | Description |
|---|---|---|
constructor | (options: TokenManagerOptions) | Accepts autoInitialize (kick off initializeSession() on construction) plus the base AsyncTokenManager options. |
getItem (abstract) | (key: string) => Promise<string | null> | Read one stored value. |
saveItem (abstract) | (key: string, value: string) => Promise<void> | Persist one value. |
saveItems (abstract) | (items: Record<string, string>) => Promise<void> | Persist several values at once. |
removeItem (abstract) | (key: string) => Promise<void> | Remove one value. |
clear (abstract) | () => Promise<void> | Clear all stored credentials. |
saveTokens | (tokens: Tokens) => Promise<void> | Persist only the auth/session/refresh tokens that changed. |
getTokens | () => Promise<Tokens> | Read the current auth/session/refresh tokens. |
initializeSession | () => Promise<void> | Inherited renewal path — refreshes an expired auth token when the refresh token is still valid; short-circuits when tokens are valid. |
You rarely construct TokenManager directly — the install scaffolds a concrete subclass (e.g. a cookie-backed server manager). Extend it by implementing the storage primitives:
import { TokenManager } from '@woographql/next'; export class ClientTokenManager extends TokenManager { async getItem(key: string) { return localStorage.getItem(key); } async saveItem(key: string, value: string) { localStorage.setItem(key, value); } async saveItems(items: Record<string, string>) { for (const [k, v] of Object.entries(items)) localStorage.setItem(k, v); } async removeItem(key: string) { localStorage.removeItem(key); } async clear() { /* remove the managed keys */ } }
The concrete class is then handed to useTokenManager on the client, or to withTokenManager / withMiddlewareTokenManager on the server.
TokenManager to the React session