fix: BetterAuth 500 - request.headers.get is not a function (Express/Web API incompatibility) #410

Closed
opened 2026-02-16 00:36:44 +00:00 by jason.woltje · 0 comments
Owner

Summary

The BetterAuth integration crashes with a 500 error when any /auth/* endpoint is hit. The root cause is a request object incompatibility between BetterAuth and NestJS/Express.

Error

TypeError: request?.headers.get is not a function
    at getBaseURL (better-auth/dist/utils/url.mjs:53:39)
    at Object.handler (better-auth/dist/auth/base.mjs:16:21)

Root Cause

BetterAuth expects a Web API Request object (Fetch API standard) which has headers.get('name'). NestJS/Express passes an IncomingMessage object which uses headers['name'] property access instead of .get().

The BetterAuth handler in the auth module needs to convert the Express request into a Web API Request (or a compatible adapter) before passing it to BetterAuth.

Steps to Reproduce

  1. Deploy the stack with OIDC_ENABLED=true
  2. Navigate to the API /auth/signin/authentik endpoint
  3. Observe 500 error

Expected Behavior

The endpoint should redirect to Authentik for OIDC authentication flow.

Affected Files

  • apps/api/src/auth/ — Auth module/controller where BetterAuth is integrated

Environment

  • NestJS with Express adapter
  • better-auth@1.4.17
## Summary The BetterAuth integration crashes with a 500 error when any `/auth/*` endpoint is hit. The root cause is a request object incompatibility between BetterAuth and NestJS/Express. ## Error ``` TypeError: request?.headers.get is not a function at getBaseURL (better-auth/dist/utils/url.mjs:53:39) at Object.handler (better-auth/dist/auth/base.mjs:16:21) ``` ## Root Cause BetterAuth expects a Web API `Request` object (Fetch API standard) which has `headers.get('name')`. NestJS/Express passes an `IncomingMessage` object which uses `headers['name']` property access instead of `.get()`. The BetterAuth handler in the auth module needs to convert the Express request into a Web API `Request` (or a compatible adapter) before passing it to BetterAuth. ## Steps to Reproduce 1. Deploy the stack with `OIDC_ENABLED=true` 2. Navigate to the API `/auth/signin/authentik` endpoint 3. Observe 500 error ## Expected Behavior The endpoint should redirect to Authentik for OIDC authentication flow. ## Affected Files - `apps/api/src/auth/` — Auth module/controller where BetterAuth is integrated ## Environment - NestJS with Express adapter - `better-auth@1.4.17`
jason.woltje added the apiapip1 labels 2026-02-16 00:36:44 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: mosaic/stack#410