Skip to content

fix(auth): restore canonical Supabase SSR pattern with x-forwarded-host - #835

Merged
tran-christian merged 1 commit into
mainfrom
fix/auth-callback-x-forwarded-host
Apr 19, 2026
Merged

fix(auth): restore canonical Supabase SSR pattern with x-forwarded-host#835
tran-christian merged 1 commit into
mainfrom
fix/auth-callback-x-forwarded-host

Conversation

@tran-christian

Copy link
Copy Markdown
Contributor

Summary

Fourth attempt at the OAuth session-cookie bug. Reverts the non-canonical Set-Cookie stamping from #832 and the earlier staged headers.append variant, returns to the canonical @supabase/ssr Next.js App Router pattern, and adds the one constraint never applied in prior attempts: x-forwarded-host handling.

Confirmed via DevTools → Application → Cookies on production www.omshub.org that the PKCE -code-verifier cookie lands but the session sb-<project>-auth-token[.0/.1] cookies do not — so the redirect response is genuinely failing to stamp session cookies on the user's domain (ruling out the HttpOnly-visibility false alarm).

Most likely root cause: on Vercel, new URL(request.url).origin resolves to an internal load-balancer host. The redirect then sends the browser to a different origin than the one @supabase/ssr stamped Set-Cookie on, so the session cookies never end up associated with www.omshub.org.

What changed

  • app/auth/callback/route.ts:
    • Use createClient from @/lib/supabase/server (already canonical) instead of an inline createServerClient
    • Remove pendingCookies[] buffer, toSetCookieHeader() helper, and response.headers.append('Set-Cookie', …) stamping
    • Add x-forwarded-host redirect branch (canonical Supabase Next.js App Router example)
    • Keep /auth/callback proxy exclusion from fix(auth): resolve PKCE code_verifier loss during Google OAuth flow #830 (already in proxy.ts)
    • Keep a single _fh diagnostic query param on successful redirect so we can confirm which branch ran in production

Why the prior attempts didn't land the fix

Neither stamping variant addressed x-forwarded-host, which is the one thing Supabase's official example has that ours did not.

@vercel

vercel Bot commented Apr 17, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
website Ready Ready Preview, Comment Apr 19, 2026 4:26pm

@tran-christian
tran-christian force-pushed the fix/auth-callback-x-forwarded-host branch from 2d8c6e0 to d575391 Compare April 19, 2026 16:25
@tran-christian
tran-christian merged commit 2520445 into main Apr 19, 2026
17 checks passed
@tran-christian
tran-christian deleted the fix/auth-callback-x-forwarded-host branch April 19, 2026 16:27
tran-christian added a commit that referenced this pull request Apr 22, 2026
…easons

Session cookies stamped after exchangeCodeForSession are Host-only by
default in @supabase/ssr 0.8. On Vercel the response host can be an
internal LB, so Set-Cookie attaches to that host even though Location
points at www.omshub.org — and the browser never sends the session back.

#835 fixed the redirect target via x-forwarded-host but did not touch
the cookie scope; the reporter confirmed in DevTools that the PKCE
code_verifier lands while sb-<ref>-auth-token[.0/.1] does not. Explicitly
set Domain to the registrable domain (omshub.org, derived from the public
host with www. stripped) so the session is valid on both www and apex
regardless of which host answered the request. Skipped for localhost /
IP literals — Host-only is correct there and some browsers reject
Domain=localhost.

Also split the error redirect into four distinguishable ?reason= values
(no_code, exchange_failed, no_session, no_pending_cookies) and add a
60-second non-HttpOnly auth_debug cookie on success so the next sign-in
attempt is conclusive from DevTools alone — removeConsole: true strips
server logs in prod.
tran-christian added a commit that referenced this pull request Apr 22, 2026
…easons (#840)

Session cookies stamped after exchangeCodeForSession are Host-only by
default in @supabase/ssr 0.8. On Vercel the response host can be an
internal LB, so Set-Cookie attaches to that host even though Location
points at www.omshub.org — and the browser never sends the session back.

#835 fixed the redirect target via x-forwarded-host but did not touch
the cookie scope; the reporter confirmed in DevTools that the PKCE
code_verifier lands while sb-<ref>-auth-token[.0/.1] does not. Explicitly
set Domain to the registrable domain (omshub.org, derived from the public
host with www. stripped) so the session is valid on both www and apex
regardless of which host answered the request. Skipped for localhost /
IP literals — Host-only is correct there and some browsers reject
Domain=localhost.

Also split the error redirect into four distinguishable ?reason= values
(no_code, exchange_failed, no_session, no_pending_cookies) and add a
60-second non-HttpOnly auth_debug cookie on success so the next sign-in
attempt is conclusive from DevTools alone — removeConsole: true strips
server logs in prod.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant