Authentication
How ProcessFlow handles user authentication using better-auth with email/password, sessions, and custom user fields.
Authentication Overview
ProcessFlow uses better-auth for all authentication. It is configured server-side in lib/auth.ts and uses the Prisma adapter, storing all auth-related data (users, sessions, accounts, verifications) directly in the same PostgreSQL database as the application data.
Configuration
The browser-side client is exported from lib/auth-client.ts and used in client components to call sign-in/sign-up methods.
Required environment variables:
| Variable | Purpose |
|---|---|
BETTER_AUTH_SECRET | Signs session tokens — generate with openssl rand -base64 32 |
BETTER_AUTH_URL | The canonical origin of the app (e.g. https://processflow.merten.tech) |
Sign-Up & Sign-In Flow
Both flows live on /authenticate (src/app/authenticate/page.tsx) as a tabbed card:
Sign-Up
- User submits email, password, and username.
authClient.signUp.email()creates theuser,account, andsessionrows via better-auth.- A server action creates a
statisticsrow for the new user (initialises gamification state). - The user is redirected to
/dashboard.
Sign-In
- User submits email and password.
authClient.signIn.email()validates credentials and issues a session cookie.- The user is redirected to
/dashboard.
Session Access in Server Actions
All Server Actions that require authentication call requireSession() from lib/session.ts:
The better-auth API route is mounted at src/app/api/auth/[...all]/route.ts and handles all auth requests (sign-in, sign-up, session refresh, sign-out).
Database Tables
better-auth manages four tables in the same PostgreSQL database:
| Table | Purpose |
|---|---|
user | User profile including custom fields (username, avatar, language, dark mode) |
session | Active sessions with expiry timestamps |
account | OAuth provider accounts (email/password uses this too) |
verification | Email verification and password reset tokens |