Better-auth setup
The auth instance is configured inpackages/auth/src/index.ts.
Core configuration
Cookie configuration
Cookies are configured based on environment:COOKIE_DOMAIN set, cookies work across subdomains (e.g., api.example.com and app.example.com).
Social providers (optional)
Discord OAuth can be enabled if credentials are provided:Database schema
Authentication tables are defined inpackages/db/src/schema/auth.ts.
User table
role field controls global admin access. Regular users have role: null.
Session table
activeOrganizationId tracks which organization the user is currently working in. This is used for multi-tenancy scoping.
Account table
Accounts link users to authentication providers:providerId is "credential" and password contains the hashed password.
Session management
Sessions are extracted from request headers in the oRPC context.Context creation
Inpackages/api/src/context.ts:
context.session.
Session structure
Authorization patterns
Procedure-level protection
Procedures use middleware for authorization checks. Public procedures - No authentication required:Resource-level authorization
Resource access is checked in service or repository layers:Organizations
Organizations provide multi-tenancy with isolated content and members.Organization table
Membership table
Users join organizations through themember table:
- owner - Full control, can delete organization
- admin - Can manage members and settings
- member - Basic access to organization content
Invitation system
New members are invited via email:Organization context
The active organization is stored in the session:Auth endpoints
Authentication endpoints are mounted at/api/auth/* in the Hono server:
POST /api/auth/sign-up/email- Register with email/passwordPOST /api/auth/sign-in/email- Sign in with email/passwordPOST /api/auth/sign-out- Sign outGET /api/auth/session- Get current sessionPOST /api/auth/organization/create- Create organizationPOST /api/auth/organization/set-active- Switch active organization- And many more…
Frontend integration
The web app uses better-auth’s React client inapps/web/src/lib/auth-client.ts:
Auth guards
Route protection usesbeforeLoad:
Organization switcher
Users can switch between organizations:Admin dashboard protection
Admin routes like Bull Board require admin role:Environment variables
Required environment variables for auth inapps/server/.env:
packages/env/src/server.ts for the full list of validated variables.