Using Custom API with Better Auth While Leveraging Session Management

Hi Better Auth community! I need help with a specific integration scenario.

My Goal:
I want to use my custom API endpoints for authentication (login/registration) but still leverage Better Auth's session management and other features. Essentially, I want Better Auth to be the frontend interface while my custom API handles the actual authentication in the background.

Current Setup:
  • Next.js application with Better Auth
  • Custom API endpoints:
    • https://domain/v1/sign-in (uses "identifier" instead of "email")
    • https://domain/v1/sign-up (uses "identifier" instead of "email")
What I'm Trying to Achieve:
  1. When a user logs in through Better Auth's interface, I want to intercept that request
  2. Route the authentication to my custom API
  3. Return the response to Better Auth
  4. Have Better Auth create and manage the session based on my API's response
What I've Tried:
I've created a custom auth provider and tried to integrate it with Better Auth, but I'm struggling with how to properly hook into Better Auth's authentication flow while using my own API endpoints.

Specific Questions:
  1. How can I properly configure Better Auth to use my custom API while still managing sessions?
  2. Is there a recommended approach for intercepting Better Auth's authentication flow?
  3. What's the best way to map my API's response format to what Better Auth expects?
I want users to interact with Better Auth's components, but have my custom API handle the actual authentication behind the scenes, while still getting all the benefits of Better Auth's session management and other features.

Any guidance would be greatly appreciated! Or if anyone can help me to provide specific repository which can solve my problem
Was this page helpful?