Architecture
Authentication is implemented as a shared package (@packages/auth) that can be used across your monorepo:
Social Providers
ZeroStarter comes pre-configured with GitHub and Google OAuth providers.- 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
- Go to Google Cloud Console
- Create a new OAuth 2.0 Client ID
- Add authorized redirect URI:
http://localhost:4000/api/auth/callback/google - Copy the Client ID and Client Secret
# Generate at https://github.com/settings/developers
GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
# Generate at https://console.cloud.google.com/apis/credentials
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_client_secret
Session Management
Better Auth handles sessions automatically with secure, HTTP-only cookies. Sessions include IP address and user agent tracking for security.
Get current session
In Server Components:app/page.tsx
Protect routes
Redirect unauthenticated users:app/(protected)/layout.tsx
Organizations & Teams
Better Auth’s organization plugin provides multi-tenant support with role-based access control.Organizations can have multiple teams, and users can be members of multiple organizations with different roles.
Database schema
The organization schema includes:- Organizations: Top-level tenant containers
- Members: Users within organizations (with roles:
member,admin, etc.) - Teams: Sub-groups within organizations
- Team Members: Users assigned to specific teams
- Invitations: Pending invitations to join organizations
packages/db/src/schema/auth.ts
Active organization tracking
Sessions track the active organization and team:packages/db/src/schema/auth.ts
Cross-Subdomain Cookies
For production deployments with multiple subdomains, ZeroStarter includes utilities for cross-subdomain cookie sharing:packages/auth/src/lib/utils.ts
Type Safety
Better Auth provides full type inference for sessions:Next Steps
Database Schema
Explore the complete authentication database schema with Drizzle ORM
Better Auth Docs
Learn more about Better Auth features and plugins