Authentication Setup
Complete guide to setting up authentication across all Turbocamp applications
Architecture Overview
Turbocamp uses a centralized authentication architecture with Better Auth:
- API App (
localhost:3002) - Primary auth server handling all authentication - Dashboard App (
localhost:3001) - Main application with full auth pages - Web App (
localhost:3000) - Marketing site with auth modal + redirects - Cross-domain cookies - Seamless auth across all domains
Required Environment Variables
Database Configuration
# Required for all apps
DATABASE_URL="postgresql://username:password@localhost:5432/turbocamp?schema=public"Install PostgreSQL locally and create a database:
createdb turbocampBetter Auth Configuration
# Required for authentication to work
BETTER_AUTH_SECRET="your-32-character-secret-key-here"Generate a secure secret:
openssl rand -base64 32Use the same BETTER_AUTH_SECRET across all applications for session sharing to work properly.
Application URLs
# API App (Primary auth server)
BETTER_AUTH_URL="http://localhost:3002"
AUTH_API_URL="http://localhost:3002"
NEXT_PUBLIC_AUTH_API_URL="http://localhost:3002"
# Application URLs
NEXT_PUBLIC_WEB_URL="http://localhost:3000"
NEXT_PUBLIC_DASHBOARD_URL="http://localhost:3001"# API App
BETTER_AUTH_URL="https://api.turbocamp.dev"
AUTH_API_URL="https://api.turbocamp.dev"
NEXT_PUBLIC_AUTH_API_URL="https://api.turbocamp.dev"
# Application URLs
NEXT_PUBLIC_WEB_URL="https://turbocamp.dev"
NEXT_PUBLIC_DASHBOARD_URL="https://dashboard.turbocamp.dev"
# Optional: Additional trusted origins
ADDITIONAL_TRUSTED_ORIGINS="https://staging.turbocamp.dev"Quick Setup
Install Dependencies
pnpm installSet Up Database
# Copy database environment
cp packages/db/.env.example packages/db/.env
# Edit packages/db/.env and add your DATABASE_URL
# Then run migrations
pnpm migrateConfigure Applications
Copy environment files for each application:
# API App (Primary auth server)
cp .env.example apps/api/.env.local
# Dashboard App
cp .env.example apps/dashboard/.env.local
# Web App
cp .env.example apps/web/.env.localEnvironment Configuration
Edit apps/api/.env.local:
DATABASE_URL="your-database-url"
BETTER_AUTH_SECRET="your-32-character-secret"
BETTER_AUTH_URL="http://localhost:3002"Edit apps/dashboard/.env.local:
AUTH_API_URL="http://localhost:3002"
NEXT_PUBLIC_AUTH_API_URL="http://localhost:3002"
NEXT_PUBLIC_DASHBOARD_URL="http://localhost:3001"
NEXT_PUBLIC_WEB_URL="http://localhost:3000"
BETTER_AUTH_SECRET="same-secret-as-api"Edit apps/web/.env.local:
AUTH_API_URL="http://localhost:3002"
NEXT_PUBLIC_AUTH_API_URL="http://localhost:3002"
NEXT_PUBLIC_WEB_URL="http://localhost:3000"
NEXT_PUBLIC_DASHBOARD_URL="http://localhost:3001"Start Development
pnpm devYour applications will be running on:
- 🌐 Web App: http://localhost:3000 (Marketing site)
- 📊 Dashboard: http://localhost:3001 (Main application)
- 🔌 API: http://localhost:3002 (Auth server)
Authentication Flow
Dual Auth Experience
Option 1: Quick Signup (Modal)
- User clicks "Get Started" on web app
- Modal opens with signup/signin tabs
- User completes authentication
- Modal closes and redirects to dashboard
Option 2: Full Page Auth (Redirect)
- User clicks "Sign In" on web app
- Redirects to dashboard login page
- User completes authentication
- Redirects to dashboard
Cross-Domain Session Management
- Primary Auth Server: API app handles all authentication
- Cross-domain Cookies: Sessions work across all subdomains
- Trusted Origins: CORS configured for secure cross-domain requests
- Automatic Redirects: Seamless experience across applications
Advanced Configuration
Email Integration
For transactional emails with Resend:
RESEND_API_KEY="re_xxxxxxxxxxxxxxxx"
RESEND_FROM="noreply@yourdomain.com"Rate Limiting
Using Upstash Redis for rate limiting:
UPSTASH_REDIS_REST_URL="https://xxxxxxxx.upstash.io"
UPSTASH_REDIS_REST_TOKEN="xxxxxxxxxxxxxxxx"Analytics
PostHog for user analytics:
NEXT_PUBLIC_POSTHOG_KEY="your-posthog-key"
NEXT_PUBLIC_POSTHOG_HOST="https://app.posthog.com"Monitoring
Sentry for error tracking:
NEXT_PUBLIC_SENTRY_DSN="https://xxxxxxxxxxxxxxxx@sentry.io/xxxxxxxx"Testing the Setup
Basic Authentication Test
- Visit http://localhost:3000
- Click "Get Started" - modal should open
- Create an account
- Should redirect to http://localhost:3001 (dashboard)
Cross-Domain Auth Test
- Visit http://localhost:3001/sign-in directly
- Sign in with your account
- Visit http://localhost:3000
- Should recognize you're already signed in
Sign Out Test
- Sign out from dashboard
- Visit web app - should not be signed in
- Cross-domain session clearing working correctly
Troubleshooting
Common Issues
Authentication not working
- ✅ Check
BETTER_AUTH_SECRETis the same across all apps - ✅ Verify
AUTH_API_URLpoints to your API app - ✅ Ensure database is running and connected
Cross-domain issues
- ✅ Check
trustedOriginsin auth configuration - ✅ Verify CORS headers are properly set
- ✅ Ensure cookies domain is configured correctly
Modal not appearing
- ✅ Check if
AuthModalis properly imported - ✅ Verify
BaseProviderincludesToastercomponent - ✅ Check for JavaScript errors in console
Debug Mode
Enable debug logging:
NODE_ENV="development"
DEBUG="better-auth:*"Production Deployment
Domain Configuration
For production, update all URLs to use your actual domains:
# Example production configuration
BETTER_AUTH_URL="https://api.turbocamp.dev"
NEXT_PUBLIC_WEB_URL="https://turbocamp.dev"
NEXT_PUBLIC_DASHBOARD_URL="https://dashboard.turbocamp.dev"Cross-Domain Cookie Setup
The system automatically handles cross-domain cookies:
- Development: Uses
localhostas cookie domain - Production: Uses
.turbocamp.devfor subdomain sharing - HTTPS Required: Secure cookies in production
- CORS Configured: Trusted origins for all your domains
DNS Configuration
Set up your DNS records:
A turbocamp.dev → Your web server IP
A dashboard.turbocamp.dev → Your dashboard server IP
A api.turbocamp.dev → Your API server IPSecurity Considerations
Required for Production
- HTTPS Only: All auth endpoints must use HTTPS
- Secure Secrets: Use strong, unique secrets
- Rate Limiting: Configure to prevent brute force attacks
- CORS Policy: Strict trusted origins configuration
- Cookie Security: Secure, HttpOnly, SameSite configured
Recommended
- Use environment-specific secrets
- Enable error monitoring with Sentry
- Set up proper logging and alerting
- Regular security audits
- Keep dependencies updated
Mobile App Integration
The centralized API is ready for mobile app integration:
REST API Endpoints
All auth operations available via REST:
POST /api/auth/sign-in- Email/password loginPOST /api/auth/sign-up- Create accountPOST /api/auth/sign-out- Sign outGET /api/auth/session- Get current sessionPOST /api/auth/reset-password- Password reset
React Native Example
// Mobile app can directly call API endpoints
const authResponse = await fetch('https://api.turbocamp.dev/api/auth/sign-in', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
});Next Steps
Once authentication is working:
- Customize UI: Update auth modal and pages with your branding
- Add OAuth: Configure Google, GitHub, etc. in Better Auth
- Email Features: Set up welcome emails and notifications
- User Profiles: Add user profile management
- Organizations: Configure team/organization features
- Mobile Apps: Integrate with React Native or other mobile frameworks
Need help? Check the Better Auth documentation or open an issue in the repository.