Better Auth
Last updated December 18, 2025
Overview
Better Auth is the default authentication provider in the scaffold. It handles email/password authentication, OAuth providers (Google, GitHub, etc.), session management, and integrates with your database for user storage.
Generate BETTER_AUTH_SECRET
Generate a secure random secret for Better Auth. This is used to encrypt sessions and tokens.
Option 1: Using OpenSSL (recommended)
openssl rand -base64 32
Option 2: Using Node.js
node -e "console.log(require('crypto').randomBytes(32).toString('base64'))"
Copy the generated secret - you'll add it to your .env.local file in the next step.
Google OAuth Setup
To enable Google sign-in, you need to create OAuth credentials in the Google Cloud Console.
Step 1: Create a Google Cloud Project
- Go to Google Cloud Console
- Click "Select a project" → "New Project"
- Enter a project name and click "Create"
Step 2: Enable Google+ API
- Navigate to "APIs & Services" → "Library"
- Search for "Google+ API" and click "Enable"
Step 3: Create OAuth Credentials
- Go to "APIs & Services" → "Credentials"
- Click "Create Credentials" → "OAuth client ID"
- If prompted, configure the OAuth consent screen first:
- Choose "External" user type
- Fill in app name, support email, developer contact
- Add scopes:
email,profile,openid - Add test users if needed
- Save and continue
- Back in "Create OAuth client ID":
- Application type: "Web application"
- Name: Your app name
- Authorized redirect URIs:
http://localhost:3000/api/auth/callback/google(for local dev) - For production, add:
https://yourdomain.com/api/auth/callback/google
- Click "Create"
- Copy the Client ID and Client Secret
Environment Variables
Add these variables to your .env.local file:
# Better Auth BETTER_AUTH_SECRET="your-generated-secret-from-step-1" # Google OAuth (optional, but recommended) GOOGLE_CLIENT_ID="your-google-client-id.apps.googleusercontent.com" GOOGLE_CLIENT_SECRET="your-google-client-secret" # App URL (required for OAuth redirects) NEXT_PUBLIC_APP_URL="http://localhost:3000"
Important:
BETTER_AUTH_SECRETis required - use the secret you generated in step 1GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRETare optional but needed for Google sign-in- For production, update
NEXT_PUBLIC_APP_URLto your production domain
Testing
- Restart your dev server:
npm run dev - Navigate to
http://localhost:3000/sign-up - Try creating an account with email/password
- Try signing in with Google (if configured)
- Verify you can access protected routes like
/dashboard
Troubleshooting:
- If Google sign-in fails, check that redirect URIs match exactly
- If sessions don't persist, verify
BETTER_AUTH_SECRETis set correctly - Check browser console and server logs for detailed error messages