docs(server): restore Better Auth integration guide for v2 - #1992
Conversation
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes — a single new doc page apps/content/docs/integrations/better-auth.mdx that restores the Better Auth integration guide for v2. It covers two patterns: resolving the session in a requireSession middleware (headers from initial context, getSession lookup, ORPCError('UNAUTHORIZED') on absence), and reusing a session already resolved outside the oRPC handler (typed as Awaited<ReturnType<typeof auth.api.getSession>>, narrowed by the middleware while public handlers check for it). Both show public plus protected procedures, inferred session types, handler setup with RPCHandler, and the cookie-header-forwarding caveat.
I verified the load-bearing claims against the repo and Better Auth's docs/source:
- The oRPC v2 API usage (
os.$context,base.middleware,base.use,next({ context })merge,RPCHandler.handlewithprefix/context) matches the existingcontext.mdx,middleware.mdx, andfetch-api.mdxdocs. auth.api.getSession({ headers })matches Better Auth's official API, and the returned type{ session, user } | null(confirmed from the better-authget-sessionroute source) lines up with theSessiontyping and both hander narrowings in the examples.- Both external links resolve and their anchors exist.
- The page is auto-discovered by Blume's filesystem content source and its
sidebar.labelmatches sibling integration pages, so no navigation config change is needed.
Technical details below only for the note about the PR description, which is editorial and non-blocking.
ℹ️ PR description says "Draft" while the PR is ready
The description still ends with "Draft while the proposed scope is being discussed.", but the PR is currently marked ready for review (draft: false). Consider updating the description to reflect the current state, e.g. noting the scope decision (docs-first, dedicated package still open per #1892).
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
Updated documentation for Better Auth integration with oRPC, clarifying session handling and middleware usage.
|
@Mnigos I just updated latest docs - review it if you have time. |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
More templates
@orpc/ai-sdk
@orpc/arktype
@orpc/bun
@orpc/client
@orpc/cloudflare
@orpc/contract
@orpc/experimental-effect
@orpc/evlog
@orpc/hibernation
@orpc/json-schema
@orpc/experimental-msw
@orpc/nest
@orpc/next
@orpc/node
@orpc/openapi
@orpc/opentelemetry
@orpc/pinia-colada
@orpc/pino
@orpc/publisher
@orpc/ratelimit
@orpc/server
@orpc/shared
@orpc/swr
@orpc/tanstack-query
@orpc/trpc
@orpc/valibot
@orpc/zod
commit: |
There was a problem hiding this comment.
ℹ️ No critical issues — two doc-completeness nits inline, plus one cross-cutting note.
Reviewed changes — a single revision commit (868c798, "Revise Better Auth integration details in documentation") on top of the previously approved page:
- Reworked the first example around the Request Headers Plugin (
context.reqHeaderswith an empty-Headersfallback), and documented the plugin-less path (server-side calls) plus the per-sub-request lookup cost in batches. - Replaced the pre-resolved-session pattern with a lazy memoized
getSessiongetter shared across a request, including batch sub-requests. - Trimmed the narrative framing (dropped the
./authimport setup sentence and the external installation/getting-headers links).
I re-verified the new load-bearing claims against repo sources and better-auth's source: RequestHeadersHandlerPluginContext / reqHeaders semantics match packages/server/src/plugins/request-headers.ts:10; per-sub-request middleware execution matches packages/server/src/plugins/batch.ts:202; server-side calls accept an initial context per client/server-side.mdx; and better-auth's get-session route returns null when the headers carry no session cookie (api/routes/session.ts), so the empty fallback yields UNAUTHORIZED rather than an error in both examples.
ℹ️ Session cookie refreshes are silently dropped in both examples
Both examples call auth.api.getSession server-side, but neither captures the response's set-cookie — in the first via the middleware, in the second via the fetch wrapper. The rewrite removed the info box that warned about exactly this for the reuse pattern, and the caveat now applies to both examples. With default better-auth settings, the sliding-session refresh never reaches the browser, so active sessions expire at their initial expiry instead of being extended. Consider restoring a short note (the removed text, plus a pointer to forwarding the response headers, e.g. via the Response Headers Plugin).
Technical details
# Better Auth response headers not forwarded to the client
## Affected sites
- apps/content/docs/integrations/better-auth.mdx — both examples call `auth.api.getSession` server-side; neither forwards the response's `set-cookie`, so sliding-session refreshes are lost.
## Required outcome
- Readers are told that (a) `getSession` may emit `set-cookie` (sliding-session refresh) and (b) the examples drop it, with a pointer to forwarding the headers (e.g. the Response Headers Plugin or better-auth's response-headers guidance).
## Suggested approach (optional)
- Restore an info box like the removed one, which referenced better-auth.com/docs/concepts/api#getting-headers alongside the framework's cookie integration.DeepSeek Flash (free via Pullfrog for OSS) | 𝕏
|
@dinwwwh Made small adjustments, now LGTM |
There was a problem hiding this comment.
ℹ️ Prior feedback largely addressed; the delta reads accurately. One earlier minor point remains open.
Reviewed changes — a single delta commit (436957b, "docs: clarify Better Auth session headers and cookies") on top of the previously reviewed revision:
- Reworded the server-side-call paragraph (
better-auth.mdx:49): the emptyHeadersfallback now correctly states thatgetSessionreturnsnullwhen no session cookie is present, rather than implying it throws — matching Better Auth's behavior. - Added a closing note (
better-auth.mdx:109) that both examples may need to forward session cookies to the browser, pointing to Better Auth's response-headers guidance. I verified the URL and its#getting-headersanchor both resolve, and the note directly addresses the cookie-forwarding concern from the prior review.
Of the feedback from the previous review, the wording nit is resolved (thread closed) and the cookie-forwarding note is addressed by the closing sentence. The one remaining item is the thread at line 12 — the example still never shows RequestHeadersHandlerPlugin registration, leaving readers on their own to enable it; that's a minor doc-completeness suggestion, not a blocker, and I'm leaving the thread open for the author to decide whether to add a pointer.
DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

Restores the Better Auth integration guide for v2 with two examples: resolving the session in protected middleware, and lazily loading a session once per request, shared across batch sub-requests. Both show public and protected procedures, inferred session types, and the lookup/error-handling tradeoffs.
Related to #1892. This is a documentation-first contribution; the dedicated package remains open for discussion.
Validation:
pnpm docs:validatepnpm --filter @orpc/content build