[PR #7894] [CLOSED] fix(api): normalize headers for server-side calls #15863

Closed
opened 2026-04-13 10:16:17 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/7894
Author: @jayy-77
Created: 2/10/2026
Status: Closed

Base: canaryHead: fix/next-headers-compat-7008


📝 Commits (1)

  • 69d8079 fix(api): normalize headers for server-side calls

📊 Changes

5 files changed (+236 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/api/to-auth-endpoints.test.ts (+65 -0)
📝 packages/better-auth/src/api/to-auth-endpoints.ts (+7 -1)
packages/better-auth/src/utils/headers.test.ts (+73 -0)
packages/better-auth/src/utils/headers.ts (+90 -0)
📝 packages/better-auth/src/utils/index.ts (+1 -0)

📄 Description

Problem

When calling endpoints via auth.api.* inside Next.js App Router server components, users often pass headers() (a ReadonlyHeaders instance). This object is not a real Web Headers, so auth.api.getSession({ headers }) could lose the cookie header and return null.

Changes

  • Normalize header-like inputs into a real Web Headers instance inside toAuthEndpoints.
  • Add a small helper toHeaders() that supports:
    • Next.js ReadonlyHeaders (entries())
    • header-like objects (get/forEach)
    • Node-style header records (including array values)
  • Add regression tests for the edge cases above.

Test plan

  • pnpm typecheck
  • pnpm -F better-auth exec vitest -t \"ReadonlyHeaders-like\"
  • pnpm -F better-auth exec vitest -t \"toHeaders\"

Fixes #7008


Summary by cubic

Fixes lost cookies when calling auth.api.* from server code (e.g., Next.js App Router headers()). Header-like inputs are now normalized to a real Web Headers so session lookups don’t return null.

  • Bug Fixes
    • Normalize inputs to Headers via toHeaders() (ReadonlyHeaders, get/forEach objects, iterables, and Node-style records with arrays).
    • Initialize internal headers before merging hook-provided headers.
    • Add regression tests for these cases. Fixes #7008.

Written for commit 69d8079c25. 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/7894 **Author:** [@jayy-77](https://github.com/jayy-77) **Created:** 2/10/2026 **Status:** ❌ Closed **Base:** `canary` ← **Head:** `fix/next-headers-compat-7008` --- ### 📝 Commits (1) - [`69d8079`](https://github.com/better-auth/better-auth/commit/69d8079c25faff23419a5415a8eb06306d33f4a5) fix(api): normalize headers for server-side calls ### 📊 Changes **5 files changed** (+236 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/api/to-auth-endpoints.test.ts` (+65 -0) 📝 `packages/better-auth/src/api/to-auth-endpoints.ts` (+7 -1) ➕ `packages/better-auth/src/utils/headers.test.ts` (+73 -0) ➕ `packages/better-auth/src/utils/headers.ts` (+90 -0) 📝 `packages/better-auth/src/utils/index.ts` (+1 -0) </details> ### 📄 Description ## Problem When calling endpoints via `auth.api.*` inside Next.js App Router server components, users often pass `headers()` (a ReadonlyHeaders instance). This object is not a real Web `Headers`, so `auth.api.getSession({ headers })` could lose the `cookie` header and return `null`. ## Changes - Normalize header-like inputs into a real Web `Headers` instance inside `toAuthEndpoints`. - Add a small helper `toHeaders()` that supports: - Next.js ReadonlyHeaders (`entries()`) - header-like objects (`get`/`forEach`) - Node-style header records (including array values) - Add regression tests for the edge cases above. ## Test plan - `pnpm typecheck` - `pnpm -F better-auth exec vitest -t \"ReadonlyHeaders-like\"` - `pnpm -F better-auth exec vitest -t \"toHeaders\"` Fixes #7008 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes lost cookies when calling auth.api.* from server code (e.g., Next.js App Router headers()). Header-like inputs are now normalized to a real Web Headers so session lookups don’t return null. - **Bug Fixes** - Normalize inputs to Headers via toHeaders() (ReadonlyHeaders, get/forEach objects, iterables, and Node-style records with arrays). - Initialize internal headers before merging hook-provided headers. - Add regression tests for these cases. Fixes #7008. <sup>Written for commit 69d8079c25faff23419a5415a8eb06306d33f4a5. 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-13 10:16:17 -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#15863