Add returnHeaders Parameter to auth.api.getSession for Improved Middleware Integration #1705

Closed
opened 2026-03-13 08:57:40 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @wh5938316 on GitHub (Aug 14, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

https://www.better-auth.com/docs/integrations/hono#middleware

When using better-auth with Hono.js and enabling cookieCache, calling auth.api.getSession in middleware should refresh the session_data. Currently, to achieve this, I have to use the asResponse: true option and manually handle the response headers, which leads to an unnecessary serialization of the response.
Here’s my current implementation in Hono.js middleware:

app.use("*", async (c, next) => {
  const response = await auth.api.getSession({
    headers: c.req.raw.headers,
    asResponse: true,
  });

  for (const [key, value] of response.headers) {
    if (key === "set-cookie") {
      c.header(key, value, { append: true });
    }
  }

  const session = await response.json();
  if (!session) {
    c.set("user", null);
    c.set("session", null);
    return next();
  }

  c.set("user", session.user);
  c.set("session", session.session);
  return next();
});

This approach is suboptimal because it requires serializing the response twice (once to extract headers and again to parse the JSON). This adds unnecessary overhead and complexity.

Current vs. Expected behavior

Add a returnHeaders parameter to auth.api.getSession that allows the method to return both the session data and the response headers in a single call, avoiding the need for asResponse: true and manual response parsing. For example:

const { session, headers } = await auth.api.getSession({
  headers: c.req.raw.headers,
  returnHeaders: true,
});

This would allow middleware to directly access the session data and headers without additional serialization, simplifying the code and improving performance.

What version of Better Auth are you using?

1.3.5

System info

System:
    OS: macOS 15.6
    CPU: (16) arm64 Apple M4 Max
    Memory: 117.80 MB / 48.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 139.0.7258.127
    Safari: 18.6

Which area(s) are affected? (Select all that apply)

Backend

Auth config (if applicable)

import { betterAuth } from "better-auth"
export const auth = betterAuth({
  emailAndPassword: {  
    enabled: true
  },
});

Additional context

No response

Originally created by @wh5938316 on GitHub (Aug 14, 2025). ### Is this suited for github? - [ ] Yes, this is suited for github ### To Reproduce https://www.better-auth.com/docs/integrations/hono#middleware When using better-auth with Hono.js and enabling cookieCache, calling auth.api.getSession in middleware should refresh the session_data. Currently, to achieve this, I have to use the asResponse: true option and manually handle the response headers, which leads to an unnecessary serialization of the response. Here’s my current implementation in Hono.js middleware: ```ts app.use("*", async (c, next) => { const response = await auth.api.getSession({ headers: c.req.raw.headers, asResponse: true, }); for (const [key, value] of response.headers) { if (key === "set-cookie") { c.header(key, value, { append: true }); } } const session = await response.json(); if (!session) { c.set("user", null); c.set("session", null); return next(); } c.set("user", session.user); c.set("session", session.session); return next(); }); ``` This approach is suboptimal because it requires serializing the response twice (once to extract headers and again to parse the JSON). This adds unnecessary overhead and complexity. ### Current vs. Expected behavior Add a returnHeaders parameter to auth.api.getSession that allows the method to return both the session data and the response headers in a single call, avoiding the need for asResponse: true and manual response parsing. For example: ```ts const { session, headers } = await auth.api.getSession({ headers: c.req.raw.headers, returnHeaders: true, }); ``` This would allow middleware to directly access the session data and headers without additional serialization, simplifying the code and improving performance. ### What version of Better Auth are you using? 1.3.5 ### System info ```bash System: OS: macOS 15.6 CPU: (16) arm64 Apple M4 Max Memory: 117.80 MB / 48.00 GB Shell: 5.9 - /bin/zsh Browsers: Chrome: 139.0.7258.127 Safari: 18.6 ``` ### Which area(s) are affected? (Select all that apply) Backend ### Auth config (if applicable) ```typescript import { betterAuth } from "better-auth" export const auth = betterAuth({ emailAndPassword: { enabled: true }, }); ``` ### Additional context _No response_
GiteaMirror added the hono label 2026-03-13 08:57:40 -05:00
Author
Owner

@frectonz commented on GitHub (Aug 14, 2025):

see #3780

@frectonz commented on GitHub (Aug 14, 2025): see #3780
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#1705