Loading…
Loading…
Server helpers that stamp the session-readiness flag the client polls to re-sync
A lightweight, non-httpOnly signal cookie (SESSION_FLAG) coordinates the client and server. The client (useSessionFlag / useTokenManager) polls its value and re-fetches the session whenever it changes — so a server-side change the browser can't observe (a cart/checkout mutation, or a guest → authenticated transition) still triggers a client re-sync. These helpers stamp a fresh value from wherever the change happens.
All are exported from @woographql/next/server.
import { SESSION_FLAG, stampSessionFlag, stampSessionFlagForRequest, stampSessionFlagForAction, } from '@woographql/next/server';
| Export | Signature | Description |
|---|---|---|
SESSION_FLAG | string ("__sessionStarted") | The signal cookie's name. |
stampSessionFlag | (response: NextResponse) => void | Stamp a fresh flag value onto a NextResponse cookie jar. |
stampSessionFlagForRequest | (request: { method: string }, response: Response, skipTimestamp?: boolean) => void | Stamp for a state-changing request — a no-op when skipTimestamp is set, the method is safe (GET/HEAD/OPTIONS), or the response can't carry cookies (a plain Response). |
stampSessionFlagForAction | (skipTimestamp?: boolean) => Promise<void> | Stamp from within a Server Action, writing via next/headers cookies (where no NextResponse jar exists). |
The flag value is unique per write (${Date.now()}-${random}), so client watchers re-sync on every change.
// In a route handler — usually via withTokenManager, which calls this for you: import { stampSessionFlagForRequest } from '@woographql/next/server'; export async function POST(request: Request) { const response = NextResponse.json({ ok: true }); stampSessionFlagForRequest(request, response); // mutates the response cookie jar return response; }
// In a login/register/logout Server Action: import { stampSessionFlagForAction } from '@woographql/next/server'; export async function loginAction(formData: FormData) { 'use server'; // …authenticate… await stampSessionFlagForAction(); // client re-syncs the now-authenticated session }
useSessionFlag reads this cookie on the clientstampSessionFlagForAction is wired