Overview
Theauth object is a Better Auth server instance that handles authentication, session management, and JWT token issuance. Itβs configured with Drizzle ORM for database persistence and supports email/password and OAuth authentication.
Configuration
Configuration Options
database
Database adapter configuration using Drizzle ORM.Drizzle adapter instance configured with your database connection
Database provider. Supported values:
"pg" (PostgreSQL), "mysql", "sqlite"Drizzle Integration
The Drizzle adapter automatically manages these tables:user- User accounts and profilessession- Active user sessionsaccount- OAuth account linksverification- Email verification tokensjwks- JSON Web Key Sets for JWT signing
src/db/schema.ts and synced using:
emailAndPassword
Enable email and password authentication.Set to
true to enable email/password sign-in and sign-up- Sign up with
authClient.signUp.email({ email, password, name }) - Sign in with
authClient.signIn.email({ email, password })
socialProviders
Configure OAuth social providers.Object containing OAuth provider configurations
Google OAuth
Google OAuth 2.0 client ID from Google Cloud Console
Google OAuth 2.0 client secret from Google Cloud Console
.env file:
Adding More Providers
Better Auth supports multiple OAuth providers:plugins
Array of Better Auth plugins to extend functionality.Array of plugin instances
JWT Plugin
Thejwt() plugin enables JWT token issuance and JWKS endpoints:
/api/auth/token- Issue JWT tokens/api/auth/jwks- JWKS public keys for verification- Token signing with Ed25519 or RS256 algorithms
baseURL
The base URL where your auth server is accessible. Used for OAuth callbacks and token validation.
API Endpoints
Better Auth automatically provides REST API endpoints through the Next.js API route atsrc/app/api/auth/[...all]/route.ts:
Available Endpoints
POST /api/auth/sign-in/email
Sign in with email and password. Request:POST /api/auth/sign-up/email
Create a new account with email and password. Request:GET /api/auth/sign-in/google
Initiate Google OAuth flow. Redirects to Google sign-in.GET /api/auth/callback/google
Google OAuth callback endpoint. Handles the OAuth response.POST /api/auth/sign-out
Sign out the current user and invalidate their session.GET /api/auth/session
Get the current session information. Response:GET /api/auth/token
Issue a JWT token for the current session. Response:GET /api/auth/jwks
Get JSON Web Key Set for JWT verification. Response:Server-Side Methods
Theauth object provides server-side methods for authentication operations.
auth.api.getToken()
Generate a JWT token for a user session (server-side).auth.api.getSession()
Get session information from a request (server-side).Environment Variables
Required environment variables for auth server configuration:PostgreSQL connection stringExample:
postgresql://user:password@localhost:5432/databaseSecret key for signing tokens and cookies. Generate a secure random string.Example:
openssl rand -base64 32Base URL of your auth serverDevelopment:
http://localhost:3000Production: https://yourdomain.comGoogle OAuth client ID (required if using Google OAuth)
Google OAuth client secret (required if using Google OAuth)
Example .env File
JWT Verification (Backend)
Backend services can verify JWT tokens using the JWKS endpoint. Here are examples in different languages:Go (using jwx)
Python (using PyJWT)
Node.js (using jose)
Security Considerations
Token Expiration
JWT tokens are short-lived by default (typically 15 minutes). Always check theexp claim:
HTTPS in Production
Always use HTTPS in production. JWT tokens are bearer tokens - anyone with the token can impersonate the user.Issuer and Audience Validation
Always validateiss and aud claims to prevent token reuse across services:
Database Security
Use strong credentials and limit database access:Secret Management
Never commit secrets to version control. Use environment variables or secret management services:Related
- Auth Client - Client-side authentication methods
- Database Setup - Drizzle ORM configuration
- Environment Variables - Complete environment setup guide