Server-Side Configuration
The main authentication configuration is insrc/lib/auth.ts:
src/lib/auth.ts
Key Configuration Options
Database Adapter
Database Adapter
Better Auth uses the Drizzle ORM adapter to connect to PostgreSQL:This automatically handles all database operations for users, sessions, accounts, and verification tokens.
Email & Password
Email & Password
Email/password authentication is enabled with a simple flag:This provides built-in endpoints for sign-up, sign-in, and password reset.
OAuth Providers
OAuth Providers
Social authentication providers are configured in the Better Auth supports many providers including Google, GitHub, Discord, and more.
socialProviders object:JWT Plugin
JWT Plugin
The JWT plugin enables token-based authentication for backend APIs:Learn more in the JWT Tokens section.
Client-Side Setup
The client-side auth configuration is insrc/lib/auth-client.ts:
src/lib/auth-client.ts
Authentication Flows
Email/Password Sign-Up
When a user signs up with email and password:- The frontend calls
authClient.signUp.email()with credentials - Better Auth validates the input and creates a new user record
- A session is created and stored in the database
- The session token is set as an HTTP-only cookie
- The user is automatically signed in
Google OAuth Flow
When a user signs in with Google:- The frontend redirects to Better Auth’s OAuth endpoint
- Better Auth redirects to Google’s authorization page
- User approves the permissions on Google
- Google redirects back to Better Auth with an authorization code
- Better Auth exchanges the code for access and ID tokens
- User profile is created or updated in the database
- A session is created and the user is signed in
Session Management
Better Auth manages sessions using HTTP-only cookies for security. Sessions are stored in the database and include:- Session token: A unique, secure token for the session
- User association: Links the session to a specific user
- Expiration time: When the session expires
- Metadata: IP address and user agent for security tracking
Accessing the Current Session
In React components, use theuseSession hook:
Security Features
HTTP-Only Cookies
Session tokens are stored in HTTP-only cookies, preventing XSS attacks from accessing them.
CSRF Protection
Better Auth includes built-in CSRF protection for all state-changing operations.
Password Hashing
Passwords are hashed using secure algorithms before storage.
Token Rotation
OAuth refresh tokens are automatically rotated for enhanced security.
Environment Variables
Required environment variables for authentication:| Variable | Description | Example |
|---|---|---|
BETTER_AUTH_URL | Server-side base URL for Better Auth | https://yourapp.com |
NEXT_PUBLIC_BETTER_AUTH_URL | Client-side base URL | https://yourapp.com |
GOOGLE_CLIENT_ID | Google OAuth client ID | 123456789-abc.apps.googleusercontent.com |
GOOGLE_CLIENT_SECRET | Google OAuth client secret | GOCSPX-... |
BETTER_AUTH_SECRET | Secret key for signing tokens | Generate with openssl rand -base64 32 |
Next Steps
JWT Tokens
Learn how JWT tokens enable backend API authentication
Database Schema
Understand the authentication database tables