[PR #8519] [MERGED] fix(oauth-provider): avoid fetch redirect CORS after login #24932

Closed
opened 2026-04-15 22:38:22 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8519
Author: @GautamBytes
Created: 3/9/2026
Status: Merged
Merged: 3/9/2026
Merged by: @himself65

Base: canaryHead: fix/prevent-cors-on-email-signin


📝 Commits (2)

  • c447ebc fix: prevent CORS on oauth-provider sign-in
  • ea98307 fixed lint

📊 Changes

5 files changed (+108 additions, -2 deletions)

View changed files

📝 packages/better-auth/src/plugins/oidc-provider/authorize.ts (+2 -1)
packages/core/src/utils/fetch-metadata.test.ts (+28 -0)
packages/core/src/utils/fetch-metadata.ts (+3 -0)
📝 packages/oauth-provider/src/authorize.ts (+3 -1)
📝 packages/oauth-provider/src/oauth.test.ts (+72 -0)

📄 Description

Summary

Fix a CORS-breaking redirect in the oauth-provider login flow and factor the fetch-metadata detection into a shared helper.

Fixes: #8503

Root Cause

After signIn.email() completed on the provider login page, oauth-provider could continue the OAuth flow with an HTTP redirect instead of a JSON redirect payload.

That works for full-page navigations, but it breaks for browser fetch() requests because the browser blocks the cross-origin redirect to the RP callback as a CORS violation.

Fix

  • Return JSON { redirect, url } for fetch-driven oauth-provider redirect steps when the request carries browser fetch metadata.
  • Keep the existing Accept: application/json fallback in oauth-provider so the current manual JSON flows still work.
  • Extract the browser fetch-metadata check into a shared helper used by:
    • oauth-provider
    • built-in OIDC provider

Summary by cubic

Prevent CORS errors after email sign-in in OAuth flows by returning a JSON redirect for browser fetch requests. Added a shared fetch-metadata check and used it in both oauth-provider and oidc-provider.

  • Bug Fixes
    • Return JSON { redirect, url } when Sec-Fetch-Mode: cors is present to avoid cross-origin 302s.
    • Keep Accept: application/json as a fallback for manual JSON flows.
    • Introduce isBrowserFetchRequest in @better-auth/core/utils/fetch-metadata and use it in oauth-provider and oidc-provider.
    • Add tests for the helper and the fetch-based OAuth flow after email sign-in.

Written for commit ea98307626. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8519 **Author:** [@GautamBytes](https://github.com/GautamBytes) **Created:** 3/9/2026 **Status:** ✅ Merged **Merged:** 3/9/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/prevent-cors-on-email-signin` --- ### 📝 Commits (2) - [`c447ebc`](https://github.com/better-auth/better-auth/commit/c447ebcdfeacffd0863fd0727b7d317c5588ba6e) fix: prevent CORS on oauth-provider sign-in - [`ea98307`](https://github.com/better-auth/better-auth/commit/ea98307626ed35807a2cfac8b53f05418b54d780) fixed lint ### 📊 Changes **5 files changed** (+108 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/plugins/oidc-provider/authorize.ts` (+2 -1) ➕ `packages/core/src/utils/fetch-metadata.test.ts` (+28 -0) ➕ `packages/core/src/utils/fetch-metadata.ts` (+3 -0) 📝 `packages/oauth-provider/src/authorize.ts` (+3 -1) 📝 `packages/oauth-provider/src/oauth.test.ts` (+72 -0) </details> ### 📄 Description ## Summary Fix a CORS-breaking redirect in the oauth-provider login flow and factor the fetch-metadata detection into a shared helper. Fixes: #8503 ## Root Cause After `signIn.email()` completed on the provider login page, oauth-provider could continue the OAuth flow with an HTTP redirect instead of a JSON redirect payload. That works for full-page navigations, but it breaks for browser `fetch()` requests because the browser blocks the cross-origin redirect to the RP callback as a CORS violation. ## Fix - Return JSON `{ redirect, url }` for fetch-driven oauth-provider redirect steps when the request carries browser fetch metadata. - Keep the existing `Accept: application/json` fallback in oauth-provider so the current manual JSON flows still work. - Extract the browser fetch-metadata check into a shared helper used by: - oauth-provider - built-in OIDC provider <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Prevent CORS errors after email sign-in in OAuth flows by returning a JSON redirect for browser fetch requests. Added a shared fetch-metadata check and used it in both `oauth-provider` and `oidc-provider`. - **Bug Fixes** - Return JSON `{ redirect, url }` when `Sec-Fetch-Mode: cors` is present to avoid cross-origin 302s. - Keep `Accept: application/json` as a fallback for manual JSON flows. - Introduce `isBrowserFetchRequest` in `@better-auth/core/utils/fetch-metadata` and use it in `oauth-provider` and `oidc-provider`. - Add tests for the helper and the fetch-based OAuth flow after email sign-in. <sup>Written for commit ea98307626ed35807a2cfac8b53f05418b54d780. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-15 22:38:22 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#24932