Skip to content

Refactor AuthPlugin to align with better-auth best practices (basePath, handler forwarding)#900

Merged
hotlong merged 2 commits intomainfrom
copilot/refactor-auth-plugin-better-auth
Mar 10, 2026
Merged

Refactor AuthPlugin to align with better-auth best practices (basePath, handler forwarding)#900
hotlong merged 2 commits intomainfrom
copilot/refactor-auth-plugin-better-auth

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 10, 2026

AuthManager hardcoded basePath: '/' and AuthPlugin compensated by manually rewriting request URLs (stripping the prefix, reconstructing a new Request with duplex: 'half'). This breaks better-auth's assumptions about path matching and is fragile.

Changes

  • auth-manager.ts: Set basePath to this.config.basePath || '/api/v1/auth' instead of '/'. Added basePath to AuthManagerOptions.
  • auth-plugin.ts: Removed 13 lines of URL rewriting. Route handler now forwards directly:
    const response = await this.authManager!.handleRequest(c.req.raw);
  • auth-manager.test.ts: Added tests verifying basePath defaults correctly and accepts custom values.
Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor AuthPlugin to align with better-auth best practices (basePath, handler forwarding)</issue_title>
<issue_description>## Problem
Current AuthPlugin implementation does not fully align with better-auth server integration best practices, causing hidden routing and parsing bugs:

  • auth-manager.ts sets basePath: '/' instead of the expected /api/v1/auth
  • auth-plugin.ts manually rewrites the request URL, stripping the API prefix. This forces manual path handling and may break better-auth assumptions about basePath

Recommended Fix

High priority—refactor the integration as follows:

  1. In auth-manager.ts, set basePath to /api/v1/auth (instead of /).
  2. In auth-plugin.ts, remove custom path rewriting. Forward the original request object directly:
    return this.authManager.handleRequest(c.req.raw);
  3. Ensure that all route registration and forwarding strictly follow better-auth's official usage and expectations.

---</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 10, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
objectstack-play Ready Ready Preview, Comment Mar 10, 2026 5:48am
spec Ready Ready Preview, Comment Mar 10, 2026 5:48am

Request Review

- Set basePath to '/api/v1/auth' (configurable) in auth-manager.ts instead of '/'
- Remove manual URL rewriting in auth-plugin.ts, forward c.req.raw directly
- Add basePath to AuthManagerOptions interface
- Add tests for basePath configuration

Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor AuthPlugin to align with better-auth best practices Refactor AuthPlugin to align with better-auth best practices (basePath, handler forwarding) Mar 10, 2026
@hotlong hotlong marked this pull request as ready for review March 10, 2026 06:03
Copilot AI review requested due to automatic review settings March 10, 2026 06:03
@hotlong hotlong merged commit 25cbf5c into main Mar 10, 2026
3 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the AuthPlugin and AuthManager to align with better-auth's proper integration model. Previously, AuthManager set basePath: '/' and AuthPlugin compensated by manually rewriting request URLs (stripping the API prefix), which was fragile and broke better-auth's path matching assumptions. The fix correctly configures better-auth's basePath to match the actual route prefix and removes the unnecessary URL rewriting.

Changes:

  • AuthManager.createAuthInstance() now uses this.config.basePath || '/api/v1/auth' instead of the hardcoded '/', and basePath is added as a documented field in AuthManagerOptions.
  • AuthPlugin.registerAuthRoutes() removes 13 lines of URL rewriting (stripping prefix, reconstructing Request, using duplex: 'half') and replaces them with a single direct this.authManager!.handleRequest(c.req.raw) call.
  • Two new tests in auth-manager.test.ts verify that basePath defaults correctly and accepts custom values.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/plugins/plugin-auth/src/auth-manager.ts Adds basePath field to AuthManagerOptions with @default '/api/v1/auth'; changes basePath in betterAuth config from hardcoded '/' to this.config.basePath || '/api/v1/auth'
packages/plugins/plugin-auth/src/auth-plugin.ts Removes manual URL rewriting; forwards c.req.raw directly to handleRequest; updates route registration comment
packages/plugins/plugin-auth/src/auth-manager.test.ts Adds basePath configuration test suite with two tests verifying default and custom basePath behavior

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor AuthPlugin to align with better-auth best practices (basePath, handler forwarding)

3 participants