Docs: Add minimal server-side session retrieval example for non-framework Node servers #2643

Closed
opened 2026-03-13 10:10:34 -05:00 by GiteaMirror · 4 comments
Owner

Originally created by @latifniz on GitHub (Jan 3, 2026).

Hi
First of all, thanks for the great work on Better Auth — the API and framework integrations are very clear.

While reading the docs, I noticed that server-side session retrieval is demonstrated only in framework-managed environments (Next.js, Nuxt, Astro, etc.), where request context and cookies are handled automatically.

For non-framework Node servers (e.g. Express, Fastify, NestJS), the same logic still applies, but it requires explicitly passing request headers to retrieve the session. This is straightforward for experienced backend developers, but can be confusing for users who are new to backend auth or coming from framework-heavy environments.

What’s currently clear in the docs

How to initialize Better Auth

How to use sessions on the client (useSession in React/Vue/etc.)

How server-side session access works within framework integrations

What’s missing (small gap)

A minimal reference example showing how to retrieve a session on the server when there is no automatic request context, e.g.:

passing req.headers explicitly

checking for an existing session

optionally attaching it to the request

Proposal (very minimal)

Add a short, non-opinionated example such as:

const session = await auth.api.getSession({
headers: req.headers
});

if (!session) {
return res.status(401).end();
}

Optionally followed by brief examples for:

Express middleware

Fastify preHandler

NestJS Guard

This would be clearly labeled as a reference example (not a required pattern or full middleware guide), and would complement the existing framework docs rather than replace them.

Questions

Would this kind of minimal server-side example be welcome in the docs?

If yes, would you prefer:

a single “Non-framework Node servers” section, or

brief examples under existing integrations?

Is it okay to include Express / Fastify / NestJS together, or should it start with one?

I’m happy to follow any preferred structure or scope before opening a PR.

Thanks!

Originally created by @latifniz on GitHub (Jan 3, 2026). Hi First of all, thanks for the great work on Better Auth — the API and framework integrations are very clear. While reading the docs, I noticed that server-side session retrieval is demonstrated only in framework-managed environments (Next.js, Nuxt, Astro, etc.), where request context and cookies are handled automatically. For non-framework Node servers (e.g. Express, Fastify, NestJS), the same logic still applies, but it requires explicitly passing request headers to retrieve the session. This is straightforward for experienced backend developers, but can be confusing for users who are new to backend auth or coming from framework-heavy environments. What’s currently clear in the docs How to initialize Better Auth How to use sessions on the client (useSession in React/Vue/etc.) How server-side session access works within framework integrations What’s missing (small gap) A minimal reference example showing how to retrieve a session on the server when there is no automatic request context, e.g.: passing req.headers explicitly checking for an existing session optionally attaching it to the request Proposal (very minimal) Add a short, non-opinionated example such as: const session = await auth.api.getSession({ headers: req.headers }); if (!session) { return res.status(401).end(); } Optionally followed by brief examples for: Express middleware Fastify preHandler NestJS Guard This would be clearly labeled as a reference example (not a required pattern or full middleware guide), and would complement the existing framework docs rather than replace them. Questions Would this kind of minimal server-side example be welcome in the docs? If yes, would you prefer: a single “Non-framework Node servers” section, or brief examples under existing integrations? Is it okay to include Express / Fastify / NestJS together, or should it start with one? I’m happy to follow any preferred structure or scope before opening a PR. Thanks!
GiteaMirror added the documentation label 2026-03-13 10:10:34 -05:00
Author
Owner

@better-auth-agent[bot] commented on GitHub (Jan 3, 2026):

Thank you for your contribution regarding the minimal server-side session retrieval example for non-framework Node servers! Based on your description, adding a concise example for users working with Express, Fastify, or NestJS could definitely help clarify session management in these environments where automatic request handling isn't available.

  1. Would a minimal example like the one you provided be welcome? Yes, such examples often help bridge gaps in documentation for developers who might be newer to backend authentication.

  2. Regarding structure: It seems a single “Non-framework Node servers” section could be beneficial. This way, users can easily find all relevant information in one place rather than having to navigate through different framework sections.

  3. Including multiple examples: It would be practical to start with a core example for one server (e.g., Express) and then include brief references for Fastify and NestJS to avoid overwhelming new users while providing valuable insights.

If you have any further questions or ideas, feel free to share! Would you like more information on how to structure your PR?

If you need more help, tag @better-auth-agent in a comment so I can respond.

DiagramDiscordGitHub

Diagram Join Star

@better-auth-agent[bot] commented on GitHub (Jan 3, 2026): Thank you for your contribution regarding the minimal server-side session retrieval example for non-framework Node servers! Based on your description, adding a concise example for users working with Express, Fastify, or NestJS could definitely help clarify session management in these environments where automatic request handling isn't available. 1. **Would a minimal example like the one you provided be welcome?** Yes, such examples often help bridge gaps in documentation for developers who might be newer to backend authentication. 2. **Regarding structure:** It seems a single “Non-framework Node servers” section could be beneficial. This way, users can easily find all relevant information in one place rather than having to navigate through different framework sections. 3. **Including multiple examples:** It would be practical to start with a core example for one server (e.g., Express) and then include brief references for Fastify and NestJS to avoid overwhelming new users while providing valuable insights. If you have any further questions or ideas, feel free to share! Would you like more information on how to structure your PR? _If you need more help, tag @better-auth-agent in a comment so I can respond._ <!-- bot:webhook reply v1 --> [Diagram](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) • [Discord](https://discord.gg/fG2XXEuQX3) • [GitHub](https://github.com/Skyvern-AI/Skyvern) [![Diagram](https://img.shields.io/badge/Diagram-2b3137?style=flat-square)](https://repodiagrams.s3.eu-north-1.amazonaws.com/skyvern_ultra_detailed_interactive.html) [![Join](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&style=flat-square)](https://discord.gg/fG2XXEuQX3) [![Star](https://img.shields.io/badge/star-181717?logo=github&logoColor=white&style=flat-square)](https://github.com/Skyvern-AI/Skyvern)
Author
Owner

@bytaesu commented on GitHub (Jan 7, 2026):

Hello @latifniz,

we'll improve the examples!

In the meantime, feel free to ask any questions in our active community. You'll find plenty of useful information there. I'll also actively respond to questions 🙂

@bytaesu commented on GitHub (Jan 7, 2026): Hello @latifniz, we'll improve the examples! In the meantime, feel free to ask any questions in our active [community](https://www.better-auth.com/community). You'll find plenty of useful information there. I'll also actively respond to questions 🙂
Author
Owner

@latifniz commented on GitHub (Jan 7, 2026):

Thanks. can i make a PR for that? I want to contribute.

@latifniz commented on GitHub (Jan 7, 2026): Thanks. can i make a PR for that? I want to contribute.
Author
Owner

@bytaesu commented on GitHub (Jan 7, 2026):

Thanks. can i make a PR for that? I want to contribute.

Sure! If you can improve anything, feel free to make a PR for the docs,
or make a PR for better-auth/examples 🤗

@bytaesu commented on GitHub (Jan 7, 2026): > Thanks. can i make a PR for that? I want to contribute. Sure! If you can improve anything, feel free to make a PR for the docs, or make a PR for [better-auth/examples](https://github.com/better-auth/examples) 🤗
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#2643