Server-only middleware wrapper that attaches session credentials to proxied WordPress requests
A server-only wrapper for a Next.js proxy/middleware handler. It constructs the app-provided TokenManager per request (from the NextRequest, so it can read request.cookies) and, for proxied WordPress AJAX / REST requests, attaches the current session + auth credentials before the request is forwarded — stamping Authorization and the session header onto the proxied request.
Because the same TokenManager class is used on the client, server, and middleware, the cookie keys and storage behavior match exactly what the other managers write; middleware just reads them off the request. It also renews an expired auth token before forwarding (a middleware request reads the cookie but never refreshes it on its own, so without this it would forward a stale token), and flushes any buffered cookie writes onto the response.
import { withMiddlewareTokenManager } from '@woographql/next/server';
| Export | Signature |
|---|---|
withMiddlewareTokenManager | <T extends TokenManager>(TokenManagerClass: new (request: NextRequest) => T, handler: (request: NextRequest, tokenManager: T) => Promise<NextResponse>, sessionHeader?: string, sessionHeaderValueFn?: (sessionToken: string) => string, skipTimestamp?: boolean) => (request: NextRequest) => Promise<NextResponse> |
| Parameter | Default | Description |
|---|---|---|
TokenManagerClass | — | Per-request manager, constructed with the NextRequest. |
handler | — | The proxy/middleware handler, receiving the request + the manager. |
sessionHeader | SESSION_HEADER | Header name used to forward the session token. |
sessionHeaderValueFn | sessionHeaderValue | Formats the session token into the header value. |
skipTimestamp | false | Whether the stamped session flag omits a fresh timestamp. |
// proxy.ts (Next.js middleware) import { withMiddlewareTokenManager } from '@woographql/next/server'; import { MiddlewareTokenManager } from '@auth/MiddlewareTokenManager'; export const middleware = withMiddlewareTokenManager( MiddlewareTokenManager, async (request, tokenManager) => { // proxy the request to WordPress with credentials already attached return proxyByWCR(request); }, );
The middleware TokenManager is generated for:
| Configuration | Value |
|---|---|
credentialStorageType | cookies |