Skip to content

fix(auth): route OAuth callback URLs through the RENDER_EXTERNAL_URL fallback - #50

Merged
YonatanHen merged 1 commit into
stagingfrom
dev/oauth-callback-url-fix
Jul 30, 2026
Merged

fix(auth): route OAuth callback URLs through the RENDER_EXTERNAL_URL fallback#50
YonatanHen merged 1 commit into
stagingfrom
dev/oauth-callback-url-fix

Conversation

@YonatanHen

Copy link
Copy Markdown
Owner

Fixes Google sign-in being rejected in production with redirect_uri_mismatch — the server was sending Google http://localhost:5173/api/v1/auth/google/callback instead of the real onrender.com URL.

Root cause

registerOAuthStrategies in routes/v1/auth.ts built PUBLIC_ORIGIN from raw process.env.PUBLIC_ORIGIN ?? 'http://localhost:5173' — completely bypassing the RENDER_EXTERNAL_URL fallback, which only lived inside env.ts's resolveFileBackedSecrets(). With PUBLIC_ORIGIN correctly left unset on Render (per its own contract — "only needed for a custom domain"), the raw read was undefined and fell straight to the hardcoded localhost default. Confirmed live by hitting GET /api/v1/auth/google on the deployed service and reading the actual redirect_uri in the resulting Location header.

Fix

Extracted resolvePublicOrigin() as a standalone function, independent of the full EnvSchema, and call it from auth.ts. It can't go through loadEnv() directly — that also requires MONGODB_URI/SESSION_SECRET, which the test harness passes to buildTestApp as explicit options rather than populating in process.env, so routing through loadEnv() at request time broke five existing tests.

Test

New regression test builds a real test app with GOOGLE_CLIENT_ID set, PUBLIC_ORIGIN unset, and RENDER_EXTERNAL_URL set to a fake onrender.com URL, then asserts the actual redirect_uri query param Google would receive resolves to that URL — reproducing the exact production failure. Lives in its own file rather than inside oauth.test.ts, because configuredProviders() caches its result in a module-level singleton on first call, and that file's own tests are already the first callers with no credentials configured.

npm run test458/458 pass, 52 files. npm run typecheck clean.

Next step

Once this merges to staging, it needs to reach master and redeploy to Render before Google sign-in actually works in production — stagingmaster PR #49 is already open and will pick this up.

…fallback

Google sign-in was rejecting every attempt in production with
redirect_uri_mismatch. The server was sending Google a callback of
http://localhost:5173/api/v1/auth/google/callback instead of the real
onrender.com URL.

Root cause: registerOAuthStrategies was built from
`process.env.PUBLIC_ORIGIN ?? 'http://localhost:5173'` directly in
routes/v1/auth.ts, bypassing the RENDER_EXTERNAL_URL fallback entirely —
that logic only lived inside env.ts's resolveFileBackedSecrets(), which
auth.ts never called. With PUBLIC_ORIGIN correctly left unset on Render
(per its own sync: false / "only needed for a custom domain" contract),
the raw process.env read was undefined and fell straight to the
hardcoded default.

Extracted resolvePublicOrigin() as a standalone function in env.ts,
independent of the full EnvSchema, and call it from auth.ts. It has to be
standalone rather than routed through loadEnv(): the full schema also
requires MONGODB_URI/SESSION_SECRET, which the test harness never
populates in process.env directly (those are passed to buildTestApp as
explicit options), so calling loadEnv() at OAuth-route time broke five
existing tests that don't need any of that to check a provider flag.

New regression test builds the actual test app with GOOGLE_CLIENT_ID set,
PUBLIC_ORIGIN unset, and RENDER_EXTERNAL_URL set to a fake onrender.com
URL, then asserts the real redirect_uri query param on the Google
redirect resolves to that URL, not localhost — reproducing the exact
failure from production. Lives in its own file: configuredProviders()
caches its result in a module-level singleton on first call, and
oauth.test.ts's own tests are already the first callers in that module
instance with no credentials configured.
@YonatanHen
YonatanHen merged commit a2503a2 into staging Jul 30, 2026
5 checks 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