(Docs) Next.js middleware (proxy) getSession code snippet not accurate #2410

Closed
opened 2026-03-13 09:51:29 -05:00 by GiteaMirror · 1 comment
Owner

Originally created by @ReservedEhlek on GitHub (Nov 27, 2025).

Is this suited for github?

  • Yes, this is suited for github

To Reproduce

Open: https://www.better-auth.com/docs/integrations/next#nextjs-16-proxy

Current vs. Expected behavior

Currently the code snippet for auth.api.getSession in middleware states:

// THIS IS NOT SECURE!
// This is the recommended approach to optimistically redirect users
// We recommend handling auth checks in each page/route

with nodejs runtime it is a valid option to use getSession in middleware (proxy) and should not be considered as not secure. This note should be deleted.

This note should only be in the snippet for getSessionCookie snippet.

What version of Better Auth are you using?

1.4.2

System info

Not relevant

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

Documentation

Auth config (if applicable)


Additional context

No response

Originally created by @ReservedEhlek on GitHub (Nov 27, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### To Reproduce Open: https://www.better-auth.com/docs/integrations/next#nextjs-16-proxy ### Current vs. Expected behavior Currently the code snippet for auth.api.getSession in middleware states: > // THIS IS NOT SECURE! > // This is the recommended approach to optimistically redirect users > // We recommend handling auth checks in each page/route with nodejs runtime it is a valid option to use getSession in middleware (proxy) and should not be considered as not secure. This note should be deleted. This note should only be in the snippet for getSessionCookie snippet. ### What version of Better Auth are you using? 1.4.2 ### System info ```bash Not relevant ``` ### Which area(s) are affected? (Select all that apply) Documentation ### Auth config (if applicable) ```typescript ``` ### Additional context _No response_
GiteaMirror added the documentation label 2026-03-13 09:51:29 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Nov 27, 2025):

Hi @ReservedEhlek! I'm Dosu and I’m helping the better-auth team.

You're right—the warning in the getSession code snippet for Next.js middleware is inaccurate for Node.js runtime. Using getSession in middleware with Node.js is secure, but it's not recommended due to performance concerns and Next.js best practices. The warning should be reworded to clarify that it's not recommended (for performance), not that it's insecure. The "not secure" warning is only appropriate for getSessionCookie, which doesn't validate the session and just checks for the presence of a cookie.

Recent discussions and feedback from the Better Auth team and community agree that the docs should be updated to reflect this distinction. A suggested comment for the getSession snippet would be:

// This is secure, but not recommended in middleware due to performance.

You can see this feedback and the rationale in this discussion and related issues.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Nov 27, 2025): <!-- Greeting --> Hi @ReservedEhlek! I'm [Dosu](https://go.dosu.dev/dosubot) and I’m helping the better-auth team. <!-- Answer --> You're right—the warning in the getSession code snippet for Next.js middleware is inaccurate for Node.js runtime. Using getSession in middleware with Node.js is secure, but it's not recommended due to performance concerns and Next.js best practices. The warning should be reworded to clarify that it's not recommended (for performance), not that it's insecure. The "not secure" warning is only appropriate for getSessionCookie, which doesn't validate the session and just checks for the presence of a cookie. Recent discussions and feedback from the Better Auth team and community agree that the docs should be updated to reflect this distinction. A suggested comment for the getSession snippet would be: ```js // This is secure, but not recommended in middleware due to performance. ``` You can see this feedback and the rationale in [this discussion](https://github.com/better-auth/better-auth/issues/6187) and [related issues](https://github.com/better-auth/better-auth/issues/5539). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/863a99e7-2e4a-469b-844f-81b1f884fde7?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/863a99e7-2e4a-469b-844f-81b1f884fde7?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/863a99e7-2e4a-469b-844f-81b1f884fde7?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/863a99e7-2e4a-469b-844f-81b1f884fde7?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/863a99e7-2e4a-469b-844f-81b1f884fde7?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/863a99e7-2e4a-469b-844f-81b1f884fde7?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/863a99e7-2e4a-469b-844f-81b1f884fde7?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/cdda13d9-dd27-4d31-b09a-5d8bec92de21/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=You%27re%20right%E2%80%94the%20warning%20in%20the%20getSession%20code%20snippet%20for%20Next.js%20middleware%20is%20inaccurate%20for%20Node.js%20runtime.%20Using%20getSession%20in%20middleware%20with%20Node.js%20is%20secure%2C%20but%20it%27s%20not%20recommended%20due%20to%20performance%20concerns%20and%20Next.js%20best%20practices.%20The%20warning%20should%20be%20reworded%20to%20clarify%20that%20it%27s%20not%20recommended%20%28for%20performance%29%2C%20not%20that%20it%27s%20insecure.%20The%20%22not%20secure%22%20warning%20is%20only%20appropriate%20for%20getSessionCookie%2C%20which%20doesn%27t%20validate%20the%20session%20and%20just%20checks%20for%20the%20presence%20of%20a%20cookie.%0A%0ARecent%20discussions%20and%20feedback%20from%20the%20Better%20Auth%20team%20and%20community%20agree%20that%20the%20docs%20should%20be%20updated%20to%20reflect%20this%20distinction.%20A%20suggested%20comment%20for%20the%20getSession%20snippet%20would%20be%3A%0A%0A%60%60%60js%0A//%20This%20is%20secure%2C%20but%20not%20recommended%20in%20middleware%20due%20to%20performance.%0A%60%60%60%0A%0AYou%20can%20see%20this%20feedback%20and%20the%20rationale%20in%20%5Bthis%20discussion%5D%28https%3A//github.com/better-auth/better-auth/issues/6187%29%20and%20%5Brelated%20issues%5D%28https%3A//github.com/better-auth/better-auth/issues/5539%29.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/better-auth/better-auth/issues/6360)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2410