[PR #6542] [CLOSED] fix: respect returnHeaders and returnStatus options in auth.api endpoints #23635

Closed
opened 2026-04-15 21:52:48 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/6542
Author: @yoshifumi-kondo
Created: 12/5/2025
Status: Closed

Base: canaryHead: fix/get-session-response-wrapping


📝 Commits (1)

  • ae4e013 fix: respect returnHeaders and returnStatus options in auth.api endpoints

📊 Changes

2 files changed (+246 additions, -26 deletions)

View changed files

📝 packages/better-auth/src/api/routes/session-api.test.ts (+208 -0)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+38 -26)

📄 Description

Summary

Fixed a bug where auth.api.getSession() and other server-side API endpoints always returned wrapped responses { headers, response, status } even when returnHeaders and returnStatus options were not specified.

Problem

In to-auth-endpoints.ts (lines 109-110), returnHeaders and returnStatus were hardcoded to true, overriding the caller's intent:

internalContext.returnHeaders = true;  // Always true
internalContext.returnStatus = true;   // Always true

This caused all auth.api.* calls to return wrapped responses instead of the actual data.

Solution

Changed lines 109-110 to respect the caller's options:

internalContext.returnHeaders = context?.returnHeaders ?? false;
internalContext.returnStatus = context?.returnStatus ?? false;

Now:

  • Without options: auth.api.getSession() returns { session, user } | null
  • With returnHeaders: true: Returns { headers, response: { session, user } }
  • With asResponse: true: Returns Response object

Changes

  • Modified packages/better-auth/src/api/to-auth-endpoints.ts (lines 109-110)
  • Added test case in packages/better-auth/src/api/routes/session-api.test.ts to verify unwrapped response behavior

Testing

  • All existing tests pass
  • Added new test case for default behavior (no wrapping)
  • Existing test case verifies returnHeaders: true still works
  • Ran pnpm format and pnpm lint - no issues

Issue

Fixes #6520

Impact

This restores the expected behavior for server-side auth.api calls, making it consistent with the documented API and type definitions.


Summary by cubic

Fixes auth.api endpoints to respect returnHeaders and returnStatus, so getSession returns unwrapped data by default. Restores the documented behavior and prevents unintended response wrapping.

  • Bug Fixes
    • Use caller-provided returnHeaders/returnStatus (default false) instead of hardcoded true.
    • getSession now returns { session, user } unless wrapping is requested.
    • Updated response handling: headers may be undefined; status defaults to 200 and uses APIError.statusCode when applicable. Tests cover default, status-only, both, and error cases.

Written for commit ae4e013ce5. Summary will update automatically 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/6542 **Author:** [@yoshifumi-kondo](https://github.com/yoshifumi-kondo) **Created:** 12/5/2025 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/get-session-response-wrapping` --- ### 📝 Commits (1) - [`ae4e013`](https://github.com/better-auth/better-auth/commit/ae4e013ce5a25056017403ce1204b39cd9aa0692) fix: respect returnHeaders and returnStatus options in auth.api endpoints ### 📊 Changes **2 files changed** (+246 additions, -26 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/routes/session-api.test.ts` (+208 -0) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+38 -26) </details> ### 📄 Description ## Summary Fixed a bug where `auth.api.getSession()` and other server-side API endpoints always returned wrapped responses `{ headers, response, status }` even when `returnHeaders` and `returnStatus` options were not specified. ## Problem In `to-auth-endpoints.ts` (lines 109-110), `returnHeaders` and `returnStatus` were hardcoded to `true`, overriding the caller's intent: ```typescript internalContext.returnHeaders = true; // Always true internalContext.returnStatus = true; // Always true ``` This caused all `auth.api.*` calls to return wrapped responses instead of the actual data. ## Solution Changed lines 109-110 to respect the caller's options: ```typescript internalContext.returnHeaders = context?.returnHeaders ?? false; internalContext.returnStatus = context?.returnStatus ?? false; ``` Now: - Without options: `auth.api.getSession()` returns `{ session, user } | null` - With `returnHeaders: true`: Returns `{ headers, response: { session, user } }` - With `asResponse: true`: Returns `Response` object ## Changes - Modified `packages/better-auth/src/api/to-auth-endpoints.ts` (lines 109-110) - Added test case in `packages/better-auth/src/api/routes/session-api.test.ts` to verify unwrapped response behavior ## Testing - [x] All existing tests pass - [x] Added new test case for default behavior (no wrapping) - [x] Existing test case verifies `returnHeaders: true` still works - [x] Ran `pnpm format` and `pnpm lint` - no issues ## Issue Fixes #6520 ## Impact This restores the expected behavior for server-side `auth.api` calls, making it consistent with the documented API and type definitions. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes auth.api endpoints to respect returnHeaders and returnStatus, so getSession returns unwrapped data by default. Restores the documented behavior and prevents unintended response wrapping. - **Bug Fixes** - Use caller-provided returnHeaders/returnStatus (default false) instead of hardcoded true. - getSession now returns { session, user } unless wrapping is requested. - Updated response handling: headers may be undefined; status defaults to 200 and uses APIError.statusCode when applicable. Tests cover default, status-only, both, and error cases. <sup>Written for commit ae4e013ce5a25056017403ce1204b39cd9aa0692. Summary will update automatically 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 21:52:48 -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#23635