Skip to content

fix(admin): forward session cookie on server-side API fetches - #493

Merged
tayebmokni merged 1 commit into
mainfrom
fix/admin-server-cookie-forwarding
May 27, 2026
Merged

fix(admin): forward session cookie on server-side API fetches#493
tayebmokni merged 1 commit into
mainfrom
fix/admin-server-cookie-forwarding

Conversation

@tayebmokni

Copy link
Copy Markdown
Contributor

Summary

Every Server Component fetch was hitting the API anonymously because the user's gonext_session cookie wasn't being forwarded — every authenticated admin page rendered "Couldn't load X (HTTP 401)" even when the operator was signed in. The browser-oriented credentials: 'include' default is a no-op on the Next.js server runtime; cookies must be pulled from the inbound request via next/headers and stamped onto the outbound fetch.

  • Adds apps/admin/src/lib/server-api.ts exposing serverApiGet<T>(path) (throws on non-2xx) and serverApiFetch(path, init?) (escape hatch returning raw Response). Both default to cache: 'no-store' so dynamic operator data never leaks across requests.
  • Refactors the 13 affected server components (posts, comments, comments/[id], users, media, media/[id], media/collections/[...slug], redirects, redirects/[id], webhooks, webhooks/[id], jobs/dlq, jobs/dlq/[id], appearance/menus) plus the shared appearance/themes/api.ts helper (and its themes/page.tsx caller) to use the new helpers. Existing graceful empty/error shapes are preserved — only the cookie-less fetch is replaced.
  • Migrates (public)/setup/page.tsx for code-path consistency even though it doesn't need a session yet.
  • (authenticated)/pages/page.tsx is currently seed-data only and has no server-side fetch to migrate.
  • Client-side files in the original audit list (media/actions.ts, appearance/themes/api-client.ts, migrate/steps/PreviewStep.tsx, migrate/steps/RunStep.tsx) already rely on credentials: 'include' from the browser — they don't have the 401 bug and are left untouched.

Test plan

  • pnpm typecheck matches the pre-existing baseline (12 unrelated MediaAsset/MenusClient errors carried in from main; zero new errors introduced).
  • pnpm exec vitest run — all 602 tests across 93 suites pass.
  • Manual smoke: sign in to the admin, open /posts, /comments, /users, /media, /redirects, /webhooks, /jobs/dlq, /appearance/themes, /appearance/menus and confirm each renders real data instead of the "Couldn't load X (HTTP 401)" state.
  • Manual smoke: hit /setup on a fresh install and confirm the wizard still loads (no regression from the GONEXT_API_URL fallback removal — NEXT_PUBLIC_API_URL is the canonical var in compose).

Signed-off-by: Tayeb Mokni tayeb.mokni@gmail.com

Every Next.js Server Component under apps/admin/src/app/(authenticated)
was issuing the GoNext API fetch without forwarding the operator's
gonext_session cookie. The browser-oriented credentials: 'include'
default does nothing on the Next.js server runtime — there is no
document.cookie jar to attach. The API auth middleware therefore saw
every list/detail request as anonymous and returned 401, leaving every
authenticated admin page stuck on its "Couldn't load X (HTTP 401)"
empty state even when the user was signed in.

Adds apps/admin/src/lib/server-api.ts exposing two helpers that pull
the inbound request's cookies via next/headers and stamp them onto the
outbound fetch:

  serverApiGet<T>(path)         — JSON GET, throws on non-2xx
  serverApiFetch(path, init?)   — escape hatch returning raw Response

Both default to cache: 'no-store' because every page they back is
operator-facing dynamic data, and a cached page would otherwise leak
one operator's view to the next.

Refactors the 13 server components and one shared themes helper that
were hand-rolling the cookie-forwarding pattern (or omitting it
entirely, as in users/page.tsx) to use the new helpers. The existing
graceful empty/error shapes are preserved so the UI continues to
render the same friendly state on non-2xx — only the cookie-less
fetch is replaced. The (public)/setup page is migrated for code-path
consistency even though it doesn't need a session.

Signed-off-by: Tayeb Mokni <tayeb.mokni@gmail.com>
@tayebmokni
tayebmokni merged commit a899ecf into main May 27, 2026
16 of 22 checks passed
@tayebmokni
tayebmokni deleted the fix/admin-server-cookie-forwarding branch May 27, 2026 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants