Installation
Better Auth requires PostgreSQL. Install
pg and @types/pg dependencies.Features
- Self-hosted authentication (full data control)
- Email/password authentication
- OAuth providers (Google, GitHub, etc.)
- Built-in authentication UI
- PostgreSQL session storage
- OAuth 2.0 with OIDC Provider plugin
- Dynamic Client Registration (DCR)
Setup
1. Create PostgreSQL Database
Create a PostgreSQL database with the OIDC Provider schema.2. Environment Variables
Create a.env file:
3. Create Middleware
Createsrc/middleware.ts:
4. Configure xmcp
Inxmcp.config.ts, enable HTTP transport:
Configuration
Required Options
| Option | Type | Description |
|---|---|---|
database | Pool | PostgreSQL connection pool (from pg package) |
baseURL | string | Base URL of your MCP server |
secret | string | Secret for signing JWT tokens (use a random string) |
providers | object | Authentication providers configuration |
Providers Configuration
Email and Password
Google OAuth
Configure Google OAuth
Configure Google OAuth
- Go to Google Cloud Console
- Create a new project or select existing
- Enable Google+ API
- Go to Credentials → Create Credentials → OAuth client ID
- Set application type to Web application
- Add authorized redirect URI:
- Development:
http://127.0.0.1:3001/auth/callback/google - Production:
https://your-domain.com/auth/callback/google
- Development:
- Copy Client ID and Client Secret to
.env
Combined Providers
You can enable both email/password and OAuth:Usage in Tools
Access Session
Get authenticated user’s session:src/tools/whoami.ts
Use in Tool Logic
src/tools/greet.ts
Authentication UI
Better Auth automatically provides a login/signup UI:- Email/password form (if
emailAndPasswordis enabled) - OAuth provider buttons (if OAuth providers are configured)
- Sign up and sign in on the same page
Customization
The UI routes are automatically mounted at/auth/*:
/auth/sign-in- Login/signup page/auth/sign-up- Alias for sign-in (same page)/auth/callback/google- Google OAuth callback/auth/callback/:provider- Generic OAuth callback
Example Project
Complete example atexamples/better-auth-http:
src/middleware.ts
src/tools/greet.ts
Database Support
Using PostgreSQL
Connection Pool Configuration
Troubleshooting
”Database connection failed”
Check yourDATABASE_URL:
- Verify host, port, database, username, and password
- Ensure PostgreSQL server is running
- Check firewall rules allow connections
- Test connection with
psqlor another client
”Tables do not exist”
Run the schema SQL script:- Copy the schema from Setup section
- Execute in your PostgreSQL database
- Verify all 7 tables are created
”Secret is required”
SetBETTER_AUTH_SECRET environment variable:
- Generate a random string (32+ characters)
- Add to
.envfile - Restart the server
Login page not found
Verify the server is running and HTTP transport is enabled:- Check
xmcp.config.tshashttp: true - Visit
http://localhost:3001/auth/sign-in - Check server logs for errors
Google OAuth redirect error
Verify redirect URI in Google Cloud Console:- Must exactly match:
http://127.0.0.1:3001/auth/callback/google - For production:
https://your-domain.com/auth/callback/google - Check
baseURLin middleware configuration