Skip to content

BetterCall does not preserve basePath when building request URLs behind reverse proxy #52

@david-yan-1

Description

@david-yan-1

🚨 BetterCall does not preserve basePath when building request URLs behind reverse proxy

Problem

When using BetterAuth/BetterCall in a Next.js app with a basePath (e.g., /other-app) and reverse proxy setup, client requests correctly hit the proxied endpoint, but inside BetterCall the basePath is stripped incorrectly. Example: Browser calls: http://localhost:3001/other-app/api/auth/get-session. Reverse proxy rewrites to: http://localhost:3004/api/auth/get-session. BetterCall internal logic currently computes:

const path = config?.basePath
  ? url.pathname
      .split(config.basePath)
      .reduce((acc, curr, index) => { ... })
      .join("")
  : url.pathname;

✅ Result: basePath is dropped: http://localhost:3004/api/auth/get-session

Expected Behavior

BetterCall should preserve the basePath in the computed path when it is missing in the proxied request. For the example above, the final request path inside BetterCall should be: /other-app/api/auth/get-session.

Proposed Fix

A simpler and more robust approach would be:

let path = url.pathname;
if (config?.basePath && !path.startsWith(config.basePath)) {
  path = `${config.basePath}${path}`;
}

Benefits: Requests already containing basePath remain unchanged, requests missing basePath get it prefixed, works correctly with reverse proxies and multi-app setups.

Environment

BetterAuth version: 1.3.26, BetterCall version: 1.0.19, Next.js version: 15.5.4

Additional Context

This affects setups where Next.js basePath is used, multiple apps are reverse-proxied, and BetterAuth client uses BetterCall internally. I can provide a minimal reproduction repo if maintainers need it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions