Overview
The Hono app uses Better Auth for authentication and session management. All authentication endpoints are mounted at/api/auth/** and handle user registration, login, session management, and OAuth providers.
Better Auth Configuration
File:src/auth/libs/index.ts:12-56
Core Configuration
ENV.APP_TITLE- Application name displayed to usersENV.BETTER_AUTH_SECRET- Secret key for token signingENV.APP_URL- Base URL for authentication callbacks
- Uses Drizzle ORM with PostgreSQL
- Maps Better Auth tables to app schema
- See Database Reference for table schemas
- Email and password authentication enabled
- OAuth providers can be configured
Rate Limiting
File:src/auth/libs/index.ts:34-40
- Server-side requests via
auth.apiare NOT rate limited - Only client-initiated requests are rate limited
- Uses database storage for distributed rate limiting
- Disabled by default in development mode
IP Address Detection
File:src/auth/libs/index.ts:47-51
- Standard proxy headers (X-Forwarded-For, X-Real-IP)
- CloudFlare headers (CF-Connecting-IP)
- Custom load balancer headers
src/core/utils/net.ts
OpenAPI Documentation
File:src/auth/libs/index.ts:41-46
/api/auth/docs
Features:
- Interactive API documentation
- Blue Planet theme
- All Better Auth endpoints documented
- Try-it-out functionality for testing
Authentication Endpoints
File:src/routes/auth.ts:10-11
All Better Auth endpoints are handled by the Better Auth request handler:
/api/auth/**
Supported Methods: GET, POST
Common Endpoints
Better Auth provides the following endpoints (non-exhaustive list):POST /api/auth/sign-up/email
Register a new user with email and password. Request Body:200 OK
POST /api/auth/sign-in/email
Sign in with email and password. Request Body:200 OK
POST /api/auth/sign-out
Sign out and invalidate the current session. Headers:200 OK
GET /api/auth/session
Get the current user’s session. Headers:200 OK
POST /api/auth/verify-email
Verify email address with verification token. Request Body:200 OK
POST /api/auth/forget-password
Request a password reset email. Request Body:200 OK
POST /api/auth/reset-password
Reset password with reset token. Request Body:200 OK
GET /api/auth/open-api/generate-schema
Generate OpenAPI schema for Better Auth endpoints. Response:200 OK
/llms-auth.txt endpoint to generate LLM-friendly documentation
File: src/routes/llms-docs.ts:97-100
OAuth Endpoints
Better Auth also provides OAuth endpoints for each configured provider:GET /api/auth/sign-in/<provider>- Initiate OAuth flowGET /api/auth/callback/<provider>- OAuth callback handler
Server-Side API
Better Auth provides a server-side API for use in backend code:auth.api.getSession
File:src/routes/middlewares/auth.ts:10
Retrieve the current session from request headers.
src/routes/middlewares/auth.ts:7-18
auth.api.generateOpenAPISchema
File:src/routes/llms-docs.ts:97
Generate OpenAPI schema for all Better Auth endpoints.
auth.handler
File:src/routes/auth.ts:11
Main request handler for all Better Auth endpoints.
request- Standard Web API Request object
Authentication Middleware
See Middlewares Reference for detailed middleware documentation.authContextMiddleware
File:src/routes/middlewares/auth.ts:7-18
Globally applied middleware that adds user and session to request context.
c.get('user')- Current user ornullc.get('session')- Current session ornull
Session Management
Sessions are managed automatically by Better Auth: Session Storage:- Sessions stored in PostgreSQL
sessiontable - See Database Schema
id- Unique session identifiertoken- Session token for authenticationexpiresAt- Session expiration timestampuserId- Reference to useripAddress- Client IP addressuserAgent- Client user agent
- Automatically managed by Better Auth
- HttpOnly cookie for security
- Secure flag in production
- SameSite attribute for CSRF protection
- Configurable session lifetime
- Automatic cleanup of expired sessions
- Refresh token support for long-lived sessions
Security Features
CSRF Protection
File:src/app.ts:60-62
Secure Headers
File:src/app.ts:63
X-Content-Type-OptionsX-Frame-OptionsX-XSS-ProtectionStrict-Transport-Security
CORS Configuration
File:src/app.ts:45-51
Password Hashing
Better Auth automatically:- Hashes passwords using bcrypt or argon2
- Validates password strength
- Prevents password reuse
- Implements secure password reset flows
Telemetry
File:src/auth/libs/index.ts:53-55
Error Handling
Authentication errors are handled by the global error handler insrc/app.ts:72-103.
Common Error Codes:
401 Unauthorized- Invalid or missing credentials403 Forbidden- Valid credentials but insufficient permissions429 Too Many Requests- Rate limit exceeded400 Bad Request- Invalid request format
