[PR #7521] [MERGED] fix(api): return Response for HTTP request contexts #24273

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

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7521
Author: @gustavovalverde
Created: 1/21/2026
Status: Merged
Merged: 3/18/2026
Merged by: @himself65

Base: canaryHead: fix/oauth-response-format


📝 Commits (1)

  • db4168d fix(api): return Response for HTTP request contexts

📊 Changes

2 files changed (+18 additions, -3 deletions)

View changed files

📝 packages/better-auth/src/api/to-auth-endpoints.test.ts (+13 -0)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+5 -3)

📄 Description

Summary

Fixes OAuth/OIDC endpoints returning wrapped JSON responses for HTTP requests.

Problem

When endpoints receive an HTTP request context (with request object), they should return a proper Response object. Previously, if asResponse wasn't explicitly set, endpoints returned { response: {...} } which broke OAuth/OIDC spec compliance.

Solution

Detect HTTP request context by checking for context.request.url and automatically return a Response object, same as when asResponse: true is set.

const hasRequest = typeof context?.request?.url === "string";
const shouldReturnResponse = Boolean(context?.asResponse) || hasRequest;

Why this approach?

  • Generic: Works for ALL endpoints with HTTP request context, not just specific paths
  • Future-proof: No maintenance needed when adding new OAuth/OIDC/OpenID4VC endpoints
  • Complete: Handles both before hook early returns and final response construction
  • Tested: Includes regression test coverage

Fixes #7355


Summary by cubic

Return proper Response objects for OAuth/OIDC endpoints when called with an HTTP request context. This restores spec-compliant JSON bodies and Set-Cookie headers, and removes the wrapped { response: ... } output.

  • Bug Fixes
    • Detect HTTP request contexts via context.request instanceof Request and set shouldReturnResponse = context.asResponse ?? hasRequest.
    • Apply shouldReturnResponse across before-hook early returns, API error handling, and final response creation; add a regression test asserting Response type, JSON body, and Set-Cookie headers.

Written for commit db4168d236. 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/7521 **Author:** [@gustavovalverde](https://github.com/gustavovalverde) **Created:** 1/21/2026 **Status:** ✅ Merged **Merged:** 3/18/2026 **Merged by:** [@himself65](https://github.com/himself65) **Base:** `canary` ← **Head:** `fix/oauth-response-format` --- ### 📝 Commits (1) - [`db4168d`](https://github.com/better-auth/better-auth/commit/db4168d236ad54c8a48c4ab1a134d2f59ae619e6) fix(api): return Response for HTTP request contexts ### 📊 Changes **2 files changed** (+18 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/to-auth-endpoints.test.ts` (+13 -0) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+5 -3) </details> ### 📄 Description ## Summary Fixes OAuth/OIDC endpoints returning wrapped JSON responses for HTTP requests. ## Problem When endpoints receive an HTTP request context (with `request` object), they should return a proper `Response` object. Previously, if `asResponse` wasn't explicitly set, endpoints returned `{ response: {...} }` which broke OAuth/OIDC spec compliance. ## Solution Detect HTTP request context by checking for `context.request.url` and automatically return a `Response` object, same as when `asResponse: true` is set. ```typescript const hasRequest = typeof context?.request?.url === "string"; const shouldReturnResponse = Boolean(context?.asResponse) || hasRequest; ``` ## Why this approach? - **Generic**: Works for ALL endpoints with HTTP request context, not just specific paths - **Future-proof**: No maintenance needed when adding new OAuth/OIDC/OpenID4VC endpoints - **Complete**: Handles both before hook early returns and final response construction - **Tested**: Includes regression test coverage Fixes #7355 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Return proper Response objects for OAuth/OIDC endpoints when called with an HTTP request context. This restores spec-compliant JSON bodies and Set-Cookie headers, and removes the wrapped { response: ... } output. - **Bug Fixes** - Detect HTTP request contexts via context.request instanceof Request and set shouldReturnResponse = context.asResponse ?? hasRequest. - Apply shouldReturnResponse across before-hook early returns, API error handling, and final response creation; add a regression test asserting Response type, JSON body, and Set-Cookie headers. <sup>Written for commit db4168d236ad54c8a48c4ab1a134d2f59ae619e6. 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:16:33 -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#24273