Turn messy meeting notes into a short summary and a clear list of action items.
BriefAI is a small but complete AI SaaS starter. It shows the parts every real AI product needs: sign up and login, a database, a protected dashboard, a server-side AI call with structured output, input validation, and a free-plan usage limit.
Built for the YouTube tutorial on @osamaayubdev: watch the build. The code exactly as shown in the video is on the video-version branch; main has the refactored, production-style architecture described below.
- Email and password auth (bcrypt hashing, signed JWT in an httpOnly cookie)
- Route protection with Next.js
proxy.ts, plus a real check on the server for every action - Race-safe daily limits, idempotent submits, structured logging, security headers, unit tests and CI
- SQLite locally, Turso (libSQL) in production, with Drizzle ORM
- OpenAI Responses API with structured output (Zod schema): title, summary, action items
- Your API key stays on the server and never reaches the browser
- Free plan: 5 briefs per day per user (configurable)
- Demo mode: runs without an OpenAI key so anyone can try it locally
- Server Actions with
useActionState, form errors that keep what the user typed - Cache Components and Suspense streaming (Next.js 16 defaults)
- Tailwind CSS v4
Next.js 16 (App Router) · React 19 · TypeScript · Tailwind CSS v4 · Drizzle ORM · libSQL / Turso · OpenAI SDK · Zod · jose · bcryptjs
git clone https://github.com/osamaayub/briefai.git
cd briefai
npm install
cp .env.example .env.local # set SESSION_SECRET (openssl rand -base64 32)
npm run db:migrate # create the tables from drizzle/ migrations
npm run devOpen http://localhost:3000. Without OPENAI_API_KEY the app runs in demo mode, which uses a simple local rule instead of AI.
| Script | What it does |
|---|---|
npm run dev / build / start |
Next.js |
npm run check |
Lint, type-check and unit tests |
npm test |
Vitest unit tests |
npm run db:generate |
Create a new SQL migration after editing the schema |
npm run db:migrate |
Apply migrations |
npm run db:studio |
Browse the database |
Validated with Zod on first use (src/server/env.ts). A missing or invalid value fails fast with a clear message.
| Name | Required | Description |
|---|---|---|
SESSION_SECRET |
Yes | 32+ characters, signs the session cookie |
OPENAI_API_KEY |
No | Empty means demo mode |
OPENAI_MODEL |
No | Default gpt-5-mini |
DATABASE_URL |
No | file:local.db locally, Turso URL in production |
DATABASE_AUTH_TOKEN |
Prod | Turso auth token |
FREE_DAILY_LIMIT |
No | Briefs per user per day, default 5 |
Feature-first structure with clear layers. Each layer only depends on the one below it.
src/
app/ Routes only: pages, layouts, error and loading UI
proxy.ts Optimistic redirects (no database)
components/ Shared UI (Logo, FormError, SubmitButton)
lib/result.ts Result and ActionState types
server/
env.ts Typed, validated configuration
logger.ts Structured JSON logs
db/schema.ts, client.ts Drizzle schema, indexes, client
features/
auth/
actions.ts Server Actions: signup, login, logout
schemas.ts Zod input schemas
components/ AuthForm
server/
token.ts JWT sign/verify (pure, shared with proxy)
session.ts Cookie handling
password.ts bcrypt with timing-safe failed logins
user-repository.ts All user SQL
current-user.ts Data Access Layer (request-deduped)
briefs/
actions.ts Thin Server Actions: auth, call service, refresh
schemas.ts, types.ts Input and AI output schemas, domain types
components/ GenerateForm, BriefList, BriefCard, UsageCard
server/
brief-service.ts Business rules (framework-free, unit-tested)
brief-repository.ts All brief SQL, atomic limit insert
ai/generator.ts BriefGenerator interface (port)
ai/openai-generator.ts OpenAI adapter (structured output, retries, timeout)
ai/demo-generator.ts Offline adapter
index.ts Composition root: wires adapters into the service
Form (useActionState)
-> generateBrief (Server Action) authenticate with getCurrentUser()
-> briefService.generate() validate, check limit, call AI
-> BriefGenerator.generate() OpenAI or demo, behind one interface
-> briefRepository.insertWithinLimit() count + insert in one transaction
<- Result<Brief, BriefError> expected failures are values, not exceptions
-> refresh() dashboard re-renders with the new brief
- Service layer with dependency injection.
createBriefService({ repo, generator, dailyLimit })has no Next.js, SDK or database imports, so the business rules are tested with fakes in milliseconds. - Ports and adapters for AI. Adding Anthropic or Gemini means writing one class that implements
BriefGeneratorand choosing it inai/index.ts. - Race-safe usage limits. The limit is checked before the AI call (to avoid paying for a brief the user can't keep) and enforced again atomically in a write transaction (
BEGIN IMMEDIATE). Writes are queued in-process and local SQLite waits for the lock instead of failing withSQLITE_BUSY. Eight parallel requests cannot go over the limit; a real-SQLite test proves it. - Idempotent submits. The form sends a server-made
requestIdwith each submit. A double click or a retry with the same key returns the first brief, with no second AI call and no second credit. A unique index on(user_id, request_id)backs this up at the database level. - Results, not exceptions, for expected failures (
INVALID_INPUT,LIMIT_REACHED,AI_FAILED). Unexpected errors are logged and hit the route'serror.tsx. - Security. httpOnly signed cookies, server-side authorisation in every action, ownership checks on delete, constant-time-ish failed logins (no email enumeration), input length limits, security headers,
server-onlyon every module that reads secrets or the database. - Migrations, not
push. SQL migrations are committed indrizzle/and applied in CI and deploys. A composite index on(user_id, created_at)backs the dashboard and limit queries. - CI. GitHub Actions runs lint, type-check, tests, migrations and a production build on every push.
npm testCovers the brief service (creation, validation, daily limit, per-user limits, AI failure, ownership), the demo generator, auth schemas and session tokens.
- Create a database on Turso and copy the URL and token.
- Run
npm run db:migratewithDATABASE_URLandDATABASE_AUTH_TOKENset to your Turso values. - Import the repo on Vercel and add the environment variables above.
- Set a spending limit in your OpenAI account.
- Login rate limiting (for example Upstash Ratelimit) and email verification
- Stripe subscriptions for a Pro plan with a higher limit
- Streaming responses
- Teams and shared briefs
- Export to Notion or email
MIT