fix(admin): forward session cookie on server-side API fetches - #493
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every Server Component fetch was hitting the API anonymously because the user's
gonext_sessioncookie wasn't being forwarded — every authenticated admin page rendered "Couldn't load X (HTTP 401)" even when the operator was signed in. The browser-orientedcredentials: 'include'default is a no-op on the Next.js server runtime; cookies must be pulled from the inbound request vianext/headersand stamped onto the outbound fetch.apps/admin/src/lib/server-api.tsexposingserverApiGet<T>(path)(throws on non-2xx) andserverApiFetch(path, init?)(escape hatch returning rawResponse). Both default tocache: 'no-store'so dynamic operator data never leaks across requests.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 sharedappearance/themes/api.tshelper (and itsthemes/page.tsxcaller) to use the new helpers. Existing graceful empty/error shapes are preserved — only the cookie-less fetch is replaced.(public)/setup/page.tsxfor code-path consistency even though it doesn't need a session yet.(authenticated)/pages/page.tsxis currently seed-data only and has no server-side fetch to migrate.media/actions.ts,appearance/themes/api-client.ts,migrate/steps/PreviewStep.tsx,migrate/steps/RunStep.tsx) already rely oncredentials: 'include'from the browser — they don't have the 401 bug and are left untouched.Test plan
pnpm typecheckmatches the pre-existing baseline (12 unrelatedMediaAsset/MenusClienterrors carried in frommain; zero new errors introduced).pnpm exec vitest run— all 602 tests across 93 suites pass./posts,/comments,/users,/media,/redirects,/webhooks,/jobs/dlq,/appearance/themes,/appearance/menusand confirm each renders real data instead of the "Couldn't load X (HTTP 401)" state./setupon a fresh install and confirm the wizard still loads (no regression from theGONEXT_API_URLfallback removal —NEXT_PUBLIC_API_URLis the canonical var in compose).Signed-off-by: Tayeb Mokni tayeb.mokni@gmail.com