Skip to content

fix(auth): normalize app origin to prevent magic-link path doubling#177

Merged
chanced58 merged 1 commit into
mainfrom
admin-ts
Apr 30, 2026
Merged

fix(auth): normalize app origin to prevent magic-link path doubling#177
chanced58 merged 1 commit into
mainfrom
admin-ts

Conversation

@chanced58
Copy link
Copy Markdown
Owner

Summary

  • Centralizes resolution of the public app origin in a new getAppOrigin() helper that defensively strips any trailing /auth/callback (and trailing slashes) from NEXT_PUBLIC_APP_URL / APP_URL.
  • Fixes a confirmed production bug where the env var on Render included the /auth/callback path. ${appUrl}/auth/callback in send-magic-link produced an emailRedirectTo of https://www.diamondos.app/auth/callback/auth/callback. After Supabase's verify endpoint 302'd the user to that URL with the session tokens in the hash, Next.js 404'd because no route matches the doubled path.
  • Wires the helper through all four auth callsites that touch the env var: send-magic-link, player-signup, auth/callback, set-session.

Why this matters

The previous PR (#174) added diagnostic logging to investigate a session-cookie bounce — and the diagnostics surfaced this earlier-stage 404 first. The 404 means the auth callback never even runs, so all the cookie-boundary instrumentation downstream is irrelevant until this is fixed. After this lands, redeploy and the magic-link flow should reach /auth/callback cleanly; the diagnostic logs from #174 will then tell us whether there's still a session-cookie problem to solve, or whether this fix resolves the user-facing bug entirely.

Test plan

  • Deploy this branch
  • Request a fresh magic link in a clean private window at https://www.diamondos.app/login
  • Hover the link in the email — it should read https://www.diamondos.app/auth/callback?token_hash=… (single /auth/callback)
  • Click — land on /dashboard, no 404
  • Render logs show [auth/callback] setAll, [auth/callback] returning redirect, and [middleware] /dashboard … user: <uuid> error: null
  • (Independent of this fix, you should still set NEXT_PUBLIC_APP_URL=https://www.diamondos.app on Render and confirm Supabase Site URL = https://www.diamondos.app so other parts of the system that don't go through getAppOrigin are also correct)

Follow-up

Once the bounce is fully resolved end-to-end, open a small revert PR to remove the // TEMP DIAG log statements added in #174.

🤖 Generated with Claude Code

Centralizes resolution of the public app origin in `getAppOrigin()` and
defensively strips a trailing `/auth/callback` (and trailing slashes) from
NEXT_PUBLIC_APP_URL / APP_URL.

A misconfigured env var on Render had the path `/auth/callback` baked in,
so `${appUrl}/auth/callback` in send-magic-link produced an
`emailRedirectTo` of `https://www.diamondos.app/auth/callback/auth/callback`.
Supabase passed that through as `redirect_to`, the verify endpoint 302'd
the user there with the session tokens in the hash, and the doubled path
404'd in Next.js. Users blamed the magic link; the bug was config feeding
into a string concat.

The new helper makes the bug unreachable: any reasonable env-var shape
(`https://host`, `https://host/`, `https://host/auth/callback`,
`https://host/auth/callback/`) collapses to the bare origin before the
callsite appends `/auth/callback` again.

Applied at all four auth callsites that touch the env var:
- send-magic-link (was the doubling bug)
- player-signup (same shape, would have hit the same bug)
- callback (used as redirect base)
- set-session (used as expected-origin for CSRF check)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 30, 2026

Warning

Rate limit exceeded

@chanced58 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 25 minutes and 45 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: ad226ab4-df9e-4d40-9709-4e279c63b5db

📥 Commits

Reviewing files that changed from the base of the PR and between cf51171 and 43c0200.

📒 Files selected for processing (5)
  • apps/web/src/app/api/auth/player-signup/route.ts
  • apps/web/src/app/api/auth/send-magic-link/route.ts
  • apps/web/src/app/api/auth/set-session/route.ts
  • apps/web/src/app/auth/callback/route.ts
  • apps/web/src/lib/auth/app-url.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch admin-ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 25 minutes and 45 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@chanced58 chanced58 merged commit 412eeb8 into main Apr 30, 2026
1 check passed
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.

1 participant