Skip to content

fix(oauth-provider): return JSON redirects from post-login OAuth continuation#8815

Merged
gustavovalverde merged 2 commits intomainfrom
fix/oauth-provider-cors-redirect
Mar 28, 2026
Merged

fix(oauth-provider): return JSON redirects from post-login OAuth continuation#8815
gustavovalverde merged 2 commits intomainfrom
fix/oauth-provider-cors-redirect

Conversation

@gustavovalverde
Copy link
Copy Markdown
Contributor

@gustavovalverde gustavovalverde commented Mar 28, 2026

Summary

Fixes #7041.
Closes #7280.

When authorizeEndpoint runs inside the OAuth after-hook (post sign-in), 2 bugs produce raw 302 redirects that browsers cannot follow under CORS constraints:

  • After-hook omission: the after-hook was the only authorizeEndpoint caller that did not signal a JSON-capable context. consent.ts and continue.ts already set Accept: application/json; the after-hook did not.
  • Error-path bypass: 11 early-validation branches in authorizeEndpoint threw ctx.redirect() directly, bypassing handleRedirect and its Sec-Fetch-Mode / Accept check. A client deleted or disabled between authorize and sign-in would always produce an unguarded 302.

The fix is scoped to 2 mechanisms:

  • Route every error redirect through handleRedirect, which returns JSON when the request is fetch-based and falls back to 302 for browser navigations.
  • Set Accept: application/json in the after-hook only when Sec-Fetch-Mode is not navigate, preserving 302 behavior for server-rendered form logins.
  • Add the same missing header in continue.ts:created(), matching the existing pattern in selected() and postLogin().

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 in oauth-provider sets Accept: application/json only for non-navigation requests.

  • Bug Fixes
    • Routed all early-validation redirects in authorizeEndpoint through handleRedirect to return JSON when appropriate.
    • In the after-hook (oauth.ts), detect navigations via Sec-Fetch-Mode: navigate or HTML Accept types (text/html, application/xhtml+xml); otherwise set Accept: application/json.
    • Added the missing Accept: application/json header in continue.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.

…oint

sec-fetch-mode is not set on server-side form submissions, so also
check content-type for application/x-www-form-urlencoded.
Copilot AI review requested due to automatic review settings March 28, 2026 18:28
@vercel
Copy link
Copy Markdown

vercel bot commented Mar 28, 2026

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

2 Skipped Deployments
Project Deployment Actions Updated (UTC)
better-auth-demo Ignored Ignored Mar 28, 2026 6:50pm
better-auth Skipped Skipped Mar 28, 2026 6:50pm

Request Review

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 28, 2026

Open in StackBlitz

@better-auth/api-key

npm i https://pkg.pr.new/@better-auth/api-key@8815

better-auth

npm i https://pkg.pr.new/better-auth@8815

auth

npm i https://pkg.pr.new/auth@8815

@better-auth/core

npm i https://pkg.pr.new/@better-auth/core@8815

@better-auth/drizzle-adapter

npm i https://pkg.pr.new/@better-auth/drizzle-adapter@8815

@better-auth/electron

npm i https://pkg.pr.new/@better-auth/electron@8815

@better-auth/expo

npm i https://pkg.pr.new/@better-auth/expo@8815

@better-auth/i18n

npm i https://pkg.pr.new/@better-auth/i18n@8815

@better-auth/kysely-adapter

npm i https://pkg.pr.new/@better-auth/kysely-adapter@8815

@better-auth/memory-adapter

npm i https://pkg.pr.new/@better-auth/memory-adapter@8815

@better-auth/mongo-adapter

npm i https://pkg.pr.new/@better-auth/mongo-adapter@8815

@better-auth/oauth-provider

npm i https://pkg.pr.new/@better-auth/oauth-provider@8815

@better-auth/passkey

npm i https://pkg.pr.new/@better-auth/passkey@8815

@better-auth/prisma-adapter

npm i https://pkg.pr.new/@better-auth/prisma-adapter@8815

@better-auth/redis-storage

npm i https://pkg.pr.new/@better-auth/redis-storage@8815

@better-auth/scim

npm i https://pkg.pr.new/@better-auth/scim@8815

@better-auth/sso

npm i https://pkg.pr.new/@better-auth/sso@8815

@better-auth/stripe

npm i https://pkg.pr.new/@better-auth/stripe@8815

@better-auth/telemetry

npm i https://pkg.pr.new/@better-auth/telemetry@8815

@better-auth/test-utils

npm i https://pkg.pr.new/@better-auth/test-utils@8815

commit: 316c011

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 4 files

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 authorizeEndpoint through handleRedirect, 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.

@vercel vercel bot temporarily deployed to Preview – better-auth March 28, 2026 18:50 Inactive
@gustavovalverde gustavovalverde self-assigned this Mar 28, 2026
@gustavovalverde gustavovalverde added this pull request to the merge queue Mar 28, 2026
Merged via the queue into main with commit 8afe2a7 Mar 28, 2026
27 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.

Error: Access-Control-Allow-Origin * when i was redirected from chatgpt to login screen of my app

2 participants