Overview
The Better Auth provider wraps an already-configured Better Auth server instance and exposes a typedAuthPort on ctx.ports.auth. It follows the ports & adapters pattern, providing a stable interface for authentication while letting your application own the configuration.
What this provider does:
- Wraps a Better Auth instance with a simple, stable API
- Extends
ports.authwithgetSession,getUser, andrequireUsermethods - Maintains type safety for your custom User type
- Define database schema (you own your user table)
- Define the User type (you define it in your app)
- Configure Better Auth (secrets, session strategy, etc. stay in your app)
- Implement login/signup routes (use Better Auth’s routes directly)
- Manage RBAC/permissions (that’s application logic)
Installation
Setup
1. Configure Better Auth in your app
First, set up Better Auth with your database and configuration:2. Define your ports type
Add theauth port to your application’s ports type:
3. Wire the provider into your server
Register the provider when creating your server:4. Use the auth port in middleware
Create middleware to protect routes:API Reference
AuthPort<User>
The auth port interface exposed on ctx.ports.auth:
getSession(req: Request)
Get the current session from a Request. Returnsnull if not authenticated.
getUser(req: Request)
Get the current user from a Request. Returnsnull if not authenticated.
This is a convenience method that extracts the user from the session.
requireUser(req: Request)
Require an authenticated user. Throws an error if not authenticated. Use this in middleware or use cases that require authentication.Error with message "[Auth] Unauthorized" if not authenticated.
createAuthBetterAuthProvider(auth)
Factory function that creates the provider:auth: A Better Auth server instance configured in your application