Auth API Overview
Multi-provider authentication API supporting BetterAuth, NextAuth, Clerk, and AuthKit
Auth API Overview
The Auth API provides a unified authentication layer supporting multiple providers: BetterAuth, NextAuth, Clerk, and AuthKit. Switch between providers via the AUTH_PROVIDER environment variable without changing your application code.
Supported Providers
| Provider | Description | Best For |
|---|---|---|
| BetterAuth | Modern, type-safe auth | New projects, full control |
| NextAuth | Popular OAuth solution | OAuth-heavy applications |
| Clerk | Complete auth platform | Rapid development, enterprise |
| AuthKit | WorkOS authentication | B2B SaaS, enterprise SSO |
Configuration
Set your preferred provider in .env.local:
# Choose your auth provider
AUTH_PROVIDER=betterauth # Options: betterauth, nextauth, clerk, authkit
# Provider-specific credentials
AUTH_SECRET=your-secret-key
AUTH_URL=http://localhost:3000Base URL
https://api.yourdomain.com/api/authAuthentication Methods
All providers support:
- Email/Password - Traditional credential-based auth
- OAuth - Google, GitHub, GitLab, and more
- Magic Links - Passwordless email authentication
- SSO/SAML - Enterprise single sign-on (AuthKit, Clerk)
Common Endpoints
| Endpoint | Method | Description |
|---|---|---|
/sign-up | POST | Register new user account |
/sign-in | POST | Authenticate existing user |
/sign-out | POST | End user session |
/sessions | GET/DELETE | Manage active sessions |
/password-reset | POST | Initiate password reset |
/organizations | GET/POST | Manage organizations/workspaces |
Response Format
All responses follow a consistent structure across providers:
{
"success": true,
"data": {
"user": {
"id": "usr_123",
"email": "user@example.com",
"name": "John Doe"
},
"session": {
"token": "eyJhbGciOiJIUzI1NiIs...",
"expiresAt": "2024-12-31T23:59:59Z"
}
},
"message": "Authentication successful"
}Error Handling
Standard HTTP status codes with provider-agnostic error messages:
| Status | Code | Description |
|---|---|---|
| 400 | INVALID_CREDENTIALS | Email or password incorrect |
| 401 | UNAUTHORIZED | Session expired or invalid |
| 403 | FORBIDDEN | Insufficient permissions |
| 409 | EMAIL_EXISTS | Email already registered |
| 422 | VALIDATION_ERROR | Invalid input data |
| 429 | RATE_LIMITED | Too many attempts |
Provider-Specific Notes
BetterAuth
- Self-hosted, no external dependencies
- Full database control via Drizzle schema
- Supports 2FA, passkeys, and session management
NextAuth
- Requires
NEXTAUTH_URLandNEXTAUTH_SECRET - OAuth providers configured via
providersarray - JWT or database session strategies
Clerk
- Uses Clerk's hosted authentication
- Requires
@clerk/nextjspackage - Automatic session management
AuthKit
- WorkOS-powered authentication
- Built-in enterprise SSO support
- Organization management included
Getting Started
- Configure your chosen provider in
.env.local - Set up database schema (for BetterAuth/NextAuth)
- Implement sign-up and sign-in flows
- Protect routes using the auth middleware
See individual endpoint documentation for detailed request/response examples.