You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using @thallesp/nestjs-better-auth@2.2.0 with Express 5 (via @nestjs/platform-express@11.x), all Better Auth routes return 404. This is caused by Express 5's wildcard pattern /*path behavior combined with how better-call constructs request URLs.
Environment
Package
Version
@thallesp/nestjs-better-auth
2.2.0
better-auth
1.4.6
better-call
1.1.5
@nestjs/core
11.1.9
@nestjs/common
11.1.9
@nestjs/platform-express
11.1.9
express
5.1.0
The Problem
Root Cause
In nestjs-better-auth, the auth handler is mounted using:
// From dist/index.cjs line 322this.adapter.httpAdapter.getInstance().use(`${basePath}/*path`,(req,res)=>{// ...returnhandler(req,res);});
With Express 5's /*path wildcard pattern, the request properties are set as:
req.url = / (just a slash)
req.baseUrl = /api/auth/sign-in/social (the FULL matched path)
req.originalUrl = /api/auth/sign-in/social
How better-call Handles This
In better-call@1.1.5 (file: node.js), the URL is constructed as:
Using the middleware option to fix the URL before the handler runs:
// auth.module.tsimport{Module}from"@nestjs/common";import{AuthModuleasBetterAuthModule}from"@thallesp/nestjs-better-auth";import{auth}from"./auth";
@Module({imports: [BetterAuthModule.forRoot({
auth,// Fix for Express 5: The /*path pattern sets req.url=/ and req.baseUrl=full_path// better-call concatenates baseUrl+url creating a trailing slash that causes 404// This middleware restores req.url to the full path before the handler runsmiddleware: (req,_res,next)=>{req.url=req.originalUrl;req.baseUrl="";next();},}),],exports: [BetterAuthModule],})exportclassAuthModule{}
When using
@thallesp/nestjs-better-auth@2.2.0with Express 5 (via@nestjs/platform-express@11.x), all Better Auth routes return 404. This is caused by Express 5's wildcard pattern/*pathbehavior combined with howbetter-callconstructs request URLs.Environment
@thallesp/nestjs-better-authbetter-authbetter-call@nestjs/core@nestjs/common@nestjs/platform-expressexpressThe Problem
Root Cause
In
nestjs-better-auth, the auth handler is mounted using:With Express 5's
/*pathwildcard pattern, the request properties are set as:req.url=/(just a slash)req.baseUrl=/api/auth/sign-in/social(the FULL matched path)req.originalUrl=/api/auth/sign-in/socialHow better-call Handles This
In
better-call@1.1.5(file:node.js), the URL is constructed as:This results in:
baseUrl + url=/api/auth/sign-in/social+/=/api/auth/sign-in/social/The trailing slash causes Better Auth to not recognize the route, returning 404.
Expected Behavior (Express 4 / Without Wildcard)
With
app.use('/api/auth', handler)(no wildcard):req.url=/sign-in/socialreq.baseUrl=/api/auth/api/auth+/sign-in/social=/api/auth/sign-in/social(correct!)Proof of Concept
Adding debug logging confirms the issue:
Output:
Current Workaround
Using the
middlewareoption to fix the URL before the handler runs:Better Auth Configuration (for reference)
Related Issues
/api/auth/*always return 404/*pathpattern/*pathpattern issue)Notes
better-callfix (PR Support for Database Hooks? #32) assumesreq.baseUrlis the mount point andreq.urlis the remaining path/*pathpattern violates this assumption by putting the FULL path inreq.baseUrl/*splatfor Express 5, not/*path