Server-only route-handler wrappers that inject a request-scoped TokenManager
server-only higher-order handlers that wrap a Next.js route handler and hand it a request-scoped TokenManager. On every request the wrapper resolves the app-provided server manager and runs initializeSession() (the renewal path — it refreshes an expired auth token when the refresh token is still valid, and short-circuits when tokens are valid), then stamps the session flag so client watchers re-sync after a mutation.
Use withTokenManager for plain route handlers and withTokenManagerAndParams for dynamic routes that also receive a params object. Both are imported from @woographql/next/server.
The cookie-flavor install also scaffolds an
@authentication/withTokenManagerwrapper that pre-binds the generatedServerTokenManagerto these helpers, so route code can callwithTokenManager(handler)without passing the class. This page documents the underlying library export.
import { withTokenManager, withTokenManagerAndParams } from '@woographql/next/server';
| Export | Signature | Description |
|---|---|---|
withTokenManager | <T extends TokenManager>(TokenManagerClass: new () => T, handler: (request: NextRequest, tokenManager: T) => Promise<NextResponse | Response>, skipTimestamp?: boolean) => RouteHandler | Wrap a route handler, injecting a request-scoped TokenManager. |
withTokenManagerAndParams | <T extends TokenManager, P>(TokenManagerClass: new () => T, handler: (request: NextRequest, params: P, tokenManager: T) => Promise<NextResponse>, skipTimestamp?: boolean) => RouteHandler | Same, for dynamic routes — the handler also receives the route params. |
skipTimestamp (default true) controls whether the stamped session flag includes a fresh timestamp; leave it on for idempotent reads, turn it off when a mutation must force a client re-sync.
// app/api/cart/route.ts import { NextResponse } from 'next/server'; import { withTokenManager } from '@woographql/next/server'; import { ServerTokenManager } from '@auth/ServerTokenManager'; export const POST = withTokenManager(ServerTokenManager, async (request, tokenManager) => { const { authToken } = await tokenManager.getTokens(); // forward `authToken` to WooGraphQL, mutate the cart, etc. return NextResponse.json({ ok: true }); }); // app/api/order/[id]/route.ts import { withTokenManagerAndParams } from '@woographql/next/server'; export const GET = withTokenManagerAndParams<ServerTokenManager, { id: string }>( ServerTokenManager, async (request, params, tokenManager) => { return NextResponse.json({ id: params.id }); }, );
The server TokenManager these wrappers bind to is generated for:
| Configuration | Value |
|---|---|
credentialStorageType | cookies |