Overview
ZeroStarter uses Better Auth for authentication, providing a secure and feature-rich authentication system. All authentication endpoints are mounted at/api/auth/*.
Better Auth Configuration
The authentication system is configured inpackages/auth/src/index.ts:
packages/auth/src/index.ts
Authentication Endpoints
All Better Auth endpoints are available under/api/auth/*. The router handles both GET and POST requests:
api/hono/src/routers/auth.ts
Core Endpoints
GET /api/auth/get-session
GET /api/auth/get-session
Retrieve the current authenticated session.Response:
POST /api/auth/sign-in/email
POST /api/auth/sign-in/email
Sign in with email and password.Request Body:
POST /api/auth/sign-up/email
POST /api/auth/sign-up/email
Create a new account with email and password.Request Body:
POST /api/auth/sign-out
POST /api/auth/sign-out
Sign out and invalidate the current session.Request Body:
GET /api/auth/sign-in/github
GET /api/auth/sign-in/github
Initiate GitHub OAuth flow. Redirects to GitHub for authorization.
GET /api/auth/sign-in/google
GET /api/auth/sign-in/google
Initiate Google OAuth flow. Redirects to Google for authorization.
GET /api/auth/callback/github
GET /api/auth/callback/github
GitHub OAuth callback endpoint. Handles the redirect from GitHub.
GET /api/auth/callback/google
GET /api/auth/callback/google
Google OAuth callback endpoint. Handles the redirect from Google.
Better Auth API Reference
Better Auth provides its own comprehensive API documentation. You can access it at:- Email/password authentication
- Social OAuth providers
- Session management
- Organization management
- Team management
- Email verification
- Password reset
- Account linking
Authentication Flow
Email/Password Sign Up
OAuth Sign In
Session Management
Better Auth uses secure HTTP-only cookies for session management:- Cookie Name: Configurable via
cookiePrefix(based onHONO_APP_URL) - Cookie Domain: Automatically configured for cross-subdomain support
- Security: HTTP-only, Secure (in production), SameSite=Lax
- Expiration: Configurable session duration
Session Schema
Organization & Team Support
ZeroStarter includes the Better Auth organization plugin with team support:- Multi-tenant organizations
- Team management within organizations
- Role-based access control
- Member invitations
- Organization switching
Environment Variables
Required environment variables for authentication:Base URL of your application (e.g.,
https://app.example.com)Array of trusted origins for CORS (e.g.,
["https://app.example.com"])GitHub OAuth application client ID
GitHub OAuth application client secret
Google OAuth application client ID
Google OAuth application client secret
Protected Routes
Routes under/api/v1/* require authentication and use the authMiddleware:
api/hono/src/routers/v1.ts
- Validates the session cookie
- Retrieves the session and user from the database
- Injects
sessionanduserinto the context - Returns 401 Unauthorized if authentication fails
Security Best Practices
HTTP-Only Cookies
Sessions are stored in HTTP-only cookies, preventing XSS attacks
CORS Protection
Only trusted origins can access the API
Secure Connections
Production enforces HTTPS for all authentication requests
Rate Limiting
Authentication endpoints are rate-limited to prevent brute force
Next Steps
Session API
Learn how to retrieve session data
User API
Access authenticated user information
Better Auth Docs
Explore the full Better Auth documentation