Overview
ZeroStarter uses Better Auth for authentication, providing a type-safe, modern authentication solution with built-in support for OAuth providers, session management, and organization/team features.Features
- OAuth Providers: GitHub and Google authentication out of the box
- Session Management: Secure session handling with Drizzle ORM
- Organization Support: Multi-tenant organizations and teams via Better Auth plugins
- Type Safety: Full TypeScript support with inferred types
- Cross-Subdomain Cookies: Smart cookie configuration for multi-environment deployments
- OpenAPI Integration: Auto-generated API documentation for auth endpoints
Configuration
The authentication system is configured inpackages/auth/src/index.ts:
packages/auth/src/index.ts
Environment Variables
Add these variables to your.env file:
OAuth Provider Setup
GitHub OAuth
- Go to GitHub Developer Settings
- Create a new OAuth App
- Set Authorization callback URL to
http://localhost:4000/api/auth/callback/github - Copy the Client ID and generate a Client Secret
- Add to your
.envfile
Google OAuth
- Go to Google Cloud Console
- Create a new project or select an existing one
- Enable the Google+ API
- Go to Credentials → Create Credentials → OAuth 2.0 Client ID
- Set Authorized redirect URIs to
http://localhost:4000/api/auth/callback/google - Copy the Client ID and Client Secret
- Add to your
.envfile
Session Management
Better Auth handles sessions automatically with the Drizzle adapter. Sessions are stored in PostgreSQL and include:- User ID and session token
- IP address and user agent tracking
- Automatic expiration handling
- Secure cookie-based authentication
Type-Safe Session Access
Cross-Subdomain Cookie Configuration
ZeroStarter includes smart cookie configuration for multi-environment deployments. This allows sessions to work across subdomains (e.g.,app.example.com and api.example.com).
Cookie Domain
ThegetCookieDomain utility extracts the root domain:
Cookie Prefix
ThegetCookiePrefix utility isolates cookies per environment:
API Routes
Authentication routes are mounted at/api/auth in api/hono/src/routers/auth.ts:
api/hono/src/routers/auth.ts
/api/auth/sign-in- Sign in with email/password/api/auth/sign-up- Create new account/api/auth/sign-out- Sign out current user/api/auth/callback/github- GitHub OAuth callback/api/auth/callback/google- Google OAuth callback/api/auth/get-session- Get current session/api/auth/reference- Better Auth OpenAPI documentation
Organization & Team Support
The organization plugin enables multi-tenant features:- Organization creation and management
- Team-based access control
- Member invitations
- Role-based permissions
The database schema includes
organization, member, team, teamMember, and invitation tables to support these features.Protected Routes
Use the auth middleware to protect API routes:api/hono/src/routers/v1.ts