Project: AI SaaS (Full Stack)
Level: Advanced Time: 1-2 Weeks Stack: Next.js, Postgres, Stripe, Clerk, Vercel AI SDK
Overview
Build a production-ready "Chat with PDF" SaaS platform where users pay a monthly subscription.
Architecture
Step 1: Authentication (Clerk)
Protect your routes.
bash
npm install @clerk/nextjstsx
// middleware.ts
import { authMiddleware } from "@clerk/nextjs";
export default authMiddleware({
publicRoutes: ["/", "/api/webhook/stripe"],
});Step 2: Database Schema
You need to track:
- Users (Subscription status)
- Files (Uploaded PDFs)
- Chats (History)
- Messages
sql
model User {
id String @id
stripeCustomerId String?
isPro Boolean @default(false)
}Step 3: Payments (Stripe)
Create a Checkout Session.
typescript
// app/api/stripe/route.ts
const session = await stripe.checkout.sessions.create({
line_items: [{ price: 'price_123', quantity: 1 }],
mode: 'subscription',
success_url: `${origin}/dashboard`,
metadata: { userId: user.id },
});Step 4: The RAG Pipeline
When a user uploads a file:
- Index: Read PDF -> Chunk -> Embed -> Store in Pinecone/Supabase.
- Namespace: IMPORTANT! Tag vectors with
userIdorfileIdso users can't search each other's files.
typescript
// Metadata filter is CRITICAL for security
const results = await vectorStore.similaritySearch(query, 5, {
userId: currentUser.id, // Only search my files
});Step 5: Rate Limiting
Prevent abuse.
- Free Users: 5 messages/day.
- Pro Users: 100 messages/day. Use Upstash Redis for this.
Deployment Checklist
- Environment Variables set in Vercel.
- Stripe Webhooks configured.
- Database migrations applied.
- Domain DNS configured.