Overview
ChatJS uses Better Auth for all authentication. Every user sign-in goes through an OAuth social provider — there is no email/password flow. Sessions are stored in Postgres and validated on every request via a signed cookie with a short-lived cache to reduce database load. The auth layer has two modes:- Authenticated users — signed in via GitHub, Google, or Vercel OAuth. Full access to chat history, all enabled models, and all enabled features.
- Anonymous users — no sign-in required. A credit-limited, cookie-based session lets visitors try the app without an account.
Enabling Providers
Toggle providers inchat.config.ts under the authentication key:
chat.config.ts
chat.config.ts control validation and which sign-in buttons are shown. Runtime provider registration in apps/chat/lib/auth.ts is based on provider env vars being present.
To fully disable a provider, set its toggle to false and remove its env vars.
Config validation runs at build time via bun run prebuild. A missing env var for an enabled provider will fail the build early rather than causing a runtime error.
Required environment variables
| Provider | Variable | Description |
|---|---|---|
| GitHub | AUTH_GITHUB_ID | OAuth App client ID |
| GitHub | AUTH_GITHUB_SECRET | OAuth App client secret |
AUTH_GOOGLE_ID | OAuth client ID | |
AUTH_GOOGLE_SECRET | OAuth client secret | |
| Vercel | VERCEL_APP_CLIENT_ID | Vercel integration client ID |
| Vercel | VERCEL_APP_CLIENT_SECRET | Vercel integration client secret |
Provider Setup
GitHub
- Go to GitHub → Settings → Developer Settings → OAuth Apps → New OAuth App.
- Set Homepage URL to your app URL (e.g.
https://your-domain.com). - Set Authorization callback URL to:
- Copy the Client ID and generate a Client Secret.
- Add to your environment:
- Enable in
chat.config.ts:authentication: { github: true }
For local development, create a separate GitHub OAuth App with the callback URL set to
http://localhost:3000/api/auth/callback/github.- Go to Google Cloud Console → APIs & Services → Credentials → Create Credentials → OAuth client ID.
- Choose Web application.
- Under Authorized redirect URIs add:
- Copy the Client ID and Client Secret.
- Add to your environment:
- Enable in
chat.config.ts:authentication: { google: true }
The Google OAuth consent screen must be configured and published before users outside your Google Workspace can sign in. For testing, add yourself as a test user without publishing.
Vercel
- Go to Vercel Dashboard → Settings → Integrations → Create Integration.
- Under Redirect URL add:
- Copy the Client ID and Client Secret from the integration.
- Add to your environment:
- Enable in
chat.config.ts:authentication: { vercel: true }
The Vercel provider is useful when your users are Vercel teams and you want to authenticate using their Vercel account. It is off by default.
Session Lifecycle
- User clicks Continue with [Provider] and is redirected to the provider’s authorization URL.
- After approval, the provider redirects back to
/api/auth/callback/[provider]. - Better Auth exchanges the code for tokens, upserts the user in the database, and creates a session.
- A signed HttpOnly session cookie is set on the browser.
Anonymous Users
Users can start chatting without signing in. Anonymous sessions are tracked in a browser cookie rather than the database. Configure limits inchat.config.ts:
chat.config.ts
chat.config.ts
Route Protection
Public routes (auth callbacks, shared chats, landing pages) are accessible without signing in. All other routes redirect unauthenticated users to/login.
The chat API is public at the middleware level — anonymous session validation and credit enforcement happen inside the route handler.
Adding a New OAuth Provider
Better Auth supports many OAuth 2.0 / OIDC providers (Discord, Twitter/X, Apple, LinkedIn, and more). To add one:- Add it to the Better Auth config in
apps/chat/lib/auth.tswith credentials from env vars. - Add the new env vars to
apps/chat/lib/env-schema.ts. - Add a sign-in button to
apps/chat/components/social-auth-providers.tsxusing the Better Auth client. - Register the OAuth app with the provider, setting the redirect URI to
/api/auth/callback/[provider]. - Optionally, gate it behind a new flag in
authenticationConfigSchemainapps/chat/lib/config-schema.ts.