fix(oauth-provider): return JSON redirects from post-login OAuth continuation#8815
Merged
gustavovalverde merged 2 commits intomainfrom Mar 28, 2026
Merged
fix(oauth-provider): return JSON redirects from post-login OAuth continuation#8815gustavovalverde merged 2 commits intomainfrom
gustavovalverde merged 2 commits intomainfrom
Conversation
…oint sec-fetch-mode is not set on server-side form submissions, so also check content-type for application/x-www-form-urlencoded.
|
The latest updates on your projects. Learn more about Vercel for GitHub. 2 Skipped Deployments
|
@better-auth/api-key
better-auth
auth
@better-auth/core
@better-auth/drizzle-adapter
@better-auth/electron
@better-auth/expo
@better-auth/i18n
@better-auth/kysely-adapter
@better-auth/memory-adapter
@better-auth/mongo-adapter
@better-auth/oauth-provider
@better-auth/passkey
@better-auth/prisma-adapter
@better-auth/redis-storage
@better-auth/scim
@better-auth/sso
@better-auth/stripe
@better-auth/telemetry
@better-auth/test-utils
commit: |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes OAuth-provider post-login continuation behavior so redirects produced during OAuth authorization continuation are returned as JSON for fetch/CORS contexts (instead of raw 302s that browsers can’t follow under CORS).
Changes:
- Updates the OAuth after-hook to mark non-navigation sign-in requests as JSON-capable by setting
Accept: application/json. - Routes early validation “redirect” exits in
authorizeEndpointthroughhandleRedirect, enabling JSON redirects for fetch contexts. - Adds/adjusts tests to assert JSON redirect behavior (and keeps 302 behavior for navigation/form submissions).
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/oauth-provider/src/oauth.ts | After-hook sets Accept: application/json for non-navigation requests so OAuth continuation returns JSON redirects under CORS. |
| packages/oauth-provider/src/authorize.ts | Early validation redirects now go through handleRedirect so fetch contexts get JSON redirects instead of raw 302s. |
| packages/oauth-provider/src/continue.ts | Ensures created() continuation path requests JSON redirect behavior by setting Accept: application/json. |
| packages/oauth-provider/src/oauth.test.ts | Updates existing assertions and adds coverage for JSON-redirect vs 302 behavior across fetch vs navigation contexts. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Fixes #7041.
Closes #7280.
When
authorizeEndpointruns inside the OAuth after-hook (post sign-in), 2 bugs produce raw 302 redirects that browsers cannot follow under CORS constraints:authorizeEndpointcaller that did not signal a JSON-capable context.consent.tsandcontinue.tsalready setAccept: application/json; the after-hook did not.authorizeEndpointthrewctx.redirect()directly, bypassinghandleRedirectand itsSec-Fetch-Mode/Acceptcheck. A client deleted or disabled between authorize and sign-in would always produce an unguarded 302.The fix is scoped to 2 mechanisms:
handleRedirect, which returns JSON when the request is fetch-based and falls back to 302 for browser navigations.Accept: application/jsonin the after-hook only whenSec-Fetch-Modeis notnavigate, preserving 302 behavior for server-rendered form logins.continue.ts:created(), matching the existing pattern inselected()andpostLogin().Summary by cubic
Fixes CORS failures after OAuth sign-in by returning JSON redirects for fetch-based flows while preserving 302 redirects for real navigations. Early error redirects now go through
handleRedirect, and the post-login after-hook inoauth-providersetsAccept: application/jsononly for non-navigation requests.authorizeEndpointthroughhandleRedirectto return JSON when appropriate.oauth.ts), detect navigations viaSec-Fetch-Mode: navigateor HTMLAccepttypes (text/html,application/xhtml+xml); otherwise setAccept: application/json.Accept: application/jsonheader incontinue.ts:created()and expanded tests to cover continuation and post-login redirect behavior (JSON vs 302), including client deleted/disabled cases.Written for commit 316c011. Summary will update on new commits.