[PR #7466] [CLOSED] Resolve OAuth/OIDC wrapped response bug for HTTP requests #24232

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7466
Author: @sidd190
Created: 1/19/2026
Status: Closed

Base: canaryHead: fix/oauth-provider-response-format


📝 Commits (6)

  • bd7747e fix: resolve OAuth/OIDC wrapped response bug for HTTP requests
  • d8aed5e Merge branch 'canary' into fix/oauth-provider-response-format
  • 604507a fix: update field and target specific endpoints only
  • 3011689 Merge branch 'fix/oauth-provider-response-format' of https://github.com/sidd190/better-auth into fix/oauth-provider-response-format
  • 526d906 Merge branch 'canary' into fix/oauth-provider-response-format
  • 69bb9b9 Merge branch 'canary' into fix/oauth-provider-response-format

📊 Changes

1 file changed (+11 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/api/to-auth-endpoints.ts (+11 -1)

📄 Description

Fix OAuth/OIDC wrapped response bug for HTTP requests

Fixes #7355

What changes were made and why

Modified the response handling logic in packages/better-auth/src/api/to-auth-endpoints.ts to distinguish between HTTP requests from the router and direct JavaScript API calls.

Changes:

  • Added router context detection using context._flag === "router"
  • Return proper Response objects for HTTP requests via toResponse()
  • Preserve existing wrapped format for JavaScript API calls with returnHeaders: true

The problem was :
OAuth/OIDC endpoints were returning wrapped JSON responses like {"response": {"access_token": "..."}} instead of spec-compliant top-level fields like {"access_token": "..."}. This broke OAuth2/OIDC clients that expect standard response formats according to RFC specifications.

A little context

The root cause was in the endpoint pipeline returning { response, headers, status } structures for all calls, which then got serialized directly by the router for HTTP requests. While this worked fine for internal JavaScript API calls, it violated OAuth2/OIDC specifications that require top-level JSON fields.

The better-call router sets _flag: "router" in the context for HTTP requests, allowing us to detect and handle them differently.

No breaking changes. This fix maintains full backward compatibility.

Before/After Behavior

Before (Broken):

{"response": {"issuer": "http://localhost:3000", "token_endpoint": "..."}}

After (Fixed):

{"issuer": "http://localhost:3000", "token_endpoint": "..."}

Summary by cubic

Fixed OAuth/OIDC HTTP responses to return spec-compliant top‑level JSON by returning Response objects for OAuth/OIDC endpoints on HTTP requests. JS API behavior stays the same; no breaking changes.

  • Bug Fixes
    • Detect OAuth/OIDC endpoints via context.path; return Response only when context.request is present.
    • Respect asResponse/returnHeaders for non‑OAuth/OIDC endpoints and non‑HTTP calls.
    • Removes wrapped {"response": {...}} payloads for token and discovery endpoints.

Written for commit 69bb9b9461. 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/7466 **Author:** [@sidd190](https://github.com/sidd190) **Created:** 1/19/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/oauth-provider-response-format` --- ### 📝 Commits (6) - [`bd7747e`](https://github.com/better-auth/better-auth/commit/bd7747efc9fdaddfc1288e8e25c8f809e9d19cc2) fix: resolve OAuth/OIDC wrapped response bug for HTTP requests - [`d8aed5e`](https://github.com/better-auth/better-auth/commit/d8aed5eee8aa379e894503554415c60aff0d41dd) Merge branch 'canary' into fix/oauth-provider-response-format - [`604507a`](https://github.com/better-auth/better-auth/commit/604507a6b65d1da86dd4fcab0ce8c02d9cb5e218) fix: update field and target specific endpoints only - [`3011689`](https://github.com/better-auth/better-auth/commit/30116893c895cf3c6d330dc12ac833444b82c7e6) Merge branch 'fix/oauth-provider-response-format' of https://github.com/sidd190/better-auth into fix/oauth-provider-response-format - [`526d906`](https://github.com/better-auth/better-auth/commit/526d9067823fdd33d3ac6805d78922b8709694da) Merge branch 'canary' into fix/oauth-provider-response-format - [`69bb9b9`](https://github.com/better-auth/better-auth/commit/69bb9b9461ee8b079b26fcc52902181a1147c0e5) Merge branch 'canary' into fix/oauth-provider-response-format ### 📊 Changes **1 file changed** (+11 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+11 -1) </details> ### 📄 Description # Fix OAuth/OIDC wrapped response bug for HTTP requests **Fixes #7355** ## What changes were made and why Modified the response handling logic in `packages/better-auth/src/api/to-auth-endpoints.ts` to distinguish between HTTP requests from the router and direct JavaScript API calls. **Changes:** - Added router context detection using `context._flag === "router"` - Return proper `Response` objects for HTTP requests via `toResponse()` - Preserve existing wrapped format for JavaScript API calls with `returnHeaders: true` **The problem was :** OAuth/OIDC endpoints were returning wrapped JSON responses like `{"response": {"access_token": "..."}}` instead of spec-compliant top-level fields like `{"access_token": "..."}`. This broke OAuth2/OIDC clients that expect standard response formats according to RFC specifications. ## A little context The root cause was in the endpoint pipeline returning `{ response, headers, status }` structures for all calls, which then got serialized directly by the router for HTTP requests. While this worked fine for internal JavaScript API calls, it violated OAuth2/OIDC specifications that require top-level JSON fields. The better-call router sets `_flag: "router"` in the context for HTTP requests, allowing us to detect and handle them differently. **No breaking changes.** This fix maintains full backward compatibility. ## Before/After Behavior **Before (Broken):** ```json {"response": {"issuer": "http://localhost:3000", "token_endpoint": "..."}} ``` **After (Fixed):** ```json {"issuer": "http://localhost:3000", "token_endpoint": "..."} ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixed OAuth/OIDC HTTP responses to return spec-compliant top‑level JSON by returning Response objects for OAuth/OIDC endpoints on HTTP requests. JS API behavior stays the same; no breaking changes. - **Bug Fixes** - Detect OAuth/OIDC endpoints via context.path; return Response only when context.request is present. - Respect asResponse/returnHeaders for non‑OAuth/OIDC endpoints and non‑HTTP calls. - Removes wrapped {"response": {...}} payloads for token and discovery endpoints. <sup>Written for commit 69bb9b9461ee8b079b26fcc52902181a1147c0e5. 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:14:59 -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#24232