Prompt
How to set up SaaS auth billing and hosting
Latest observation
Here’s a practical blueprint for setting up auth, billing, and hosting for a SaaS app, with a focus on getting to production quickly while keeping things maintainable.
1) Choose your stack first
A common, modern SaaS stack:
- Frontend/App: Next.js / React / Remix / SvelteKit
- Auth: Clerk, Auth0, Supabase Auth, or custom auth with NextAuth/Auth.js
- Billing: Stripe
- Database: Postgres (Supabase, Neon, RDS, Railway, etc.)
- Hosting: Vercel, Fly.io, Render, Railway, AWS, GCP, Azure
- Emails: Resend, Postmark, SendGrid
- File storage: S3, Cloudflare R2, Supabase Storage
- Analytics: PostHog, Plausible, Amplitude
If you want the fastest path:
- Next.js + Clerk + Stripe + Supabase/Neon + Vercel
2) Authentication setup
Option A: Use a managed auth provider
Best for speed and reliability.
Typical features you want:
- Email/password or magic links
- OAuth login (Google, GitHub)
- MFA optional
- Session management
- Organization/team support
- Webhooks for user created/updated events
Popular choices:
- Clerk: very SaaS-friendly, easy UI, orgs/teams built in
- Auth0: powerful but can get expensive
- Supabase Auth: good if already using Supabase
- Firebase Auth: simple, but less SaaS-org oriented
What to implement
- Sign up / login / logout
- Protected routes
- User profile table in your DB
- Optional team/org model:
usersorganizationsorganization_members
Key auth logic
- After signup, create a user record in your DB
- Store the auth provider user ID in your DB
- Use middleware/route guards to protect app pages
- Use roles/permissions if needed:
- owner
- admin
- member
3) Billing setup
Use Stripe
Stripe is the standard for SaaS billing.
What Stripe handles well
- Subscriptions
- Trials
- One-time payments
- Coupons/promo codes
- Customer portal
- Invoices
- Tax support
- Payment retries / dunning
Minimum billing flow
- User signs up
- User chooses plan
- Create Stripe customer
- Create Stripe Checkout session or subscription
- Stripe sends webhook event
- You update your DB subscription status
- App gates premium features based on billing state
Important Stripe objects
- Customer
- Product
- Price
- Subscription
- Invoice
- Checkout Session
- Webhook Events
Recommended billing approach
Use Stripe Checkout for starting subscriptions. It’s much easier than building payment forms yourself.
Webhooks you need
Listen for events like:
checkout.session.completedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.payment_failedinvoice.paid
Store in your DB
Keep a local billing table:
stripe_customer_idstripe_subscription_idplansubscription_statuscurrent_period_endcancel_at_period_end
This prevents relying only on Stripe API calls during every request.
4) Billing and auth connection
You need to connect auth users to billing customers.
Common pattern
- Your app user has
id - Auth provider has
auth_user_id - Billing record stores
user_id - Stripe customer metadata includes your internal
user_id
If using teams/orgs
Billing should often attach to the organization, not the user:
- one account pays for a team
- invite members
- seat-based pricing possible later
Plan gating
In app code, determine access by:
- user signed in?
- account active?
- subscription valid?
- correct plan tier?
- seat available?
Example access rules:
- Free plan: limited projects
- Pro plan: unlimited projects
- Team plan: collaboration features
5) Hosting setup
Simple hosting choices
Vercel
Best for:
- Next.js apps
- Easy deploys
- Preview deployments
- Edge/serverless
Good if your app is mostly web and API routes.
Fly.io / Render / Railway
Best for:
- Full-stack apps
- Long-running background jobs
- more control than Vercel
- container-based deployment
AWS/GCP/Azure
Best for:
- enterprise scale
- more infrastructure control
- more operational complexity
Typical production hosting components
- App server
- Database
- Redis (optional)
- Background worker
- Object storage
- Cron jobs
Recommended starter setup
- Web app: Vercel
- DB: Neon or Supabase Postgres
- Auth: Clerk
- Billing: Stripe
- Email: Resend
6) Environment variables you’ll need
Example:
DATABASE_URLSTRIPE_SECRET_KEYSTRIPE_WEBHOOK_SECRETNEXT_PUBLIC_STRIPE_PUBLISHABLE_KEYAUTH_SECRETAUTH_PROVIDER_CLIENT_IDAUTH_PROVIDER_CLIENT_SECRETAPP_URL
Keep secrets only in server-side env vars.
7) Webhook handling
This is critical.
Rules
- Verify webhook signatures
- Make webhook handlers idempotent
- Don’t trust client-side billing state
- Update your local DB from webhook events
Example logic
When Stripe says subscription updated:
- find user/org by Stripe customer ID
- update subscription status in DB
- set access flags accordingly
8) Access control
You need both:
- authentication = who is the user?
- authorization = what can they do?
Implement:
- middleware to block unauthenticated users
- role checks for admin pages
- billing checks for paid features
Example:
- route
/apprequires login - route
/app/billingrequires account owner/admin - route
/app/analyticsrequires paid plan
9) Security basics
Must-haves:
- HTTPS everywhere
- Hash passwords if self-hosting auth
- CSRF protection where relevant
- Secure cookies
- Session expiration/rotation
- Webhook signature verification
- Rate limiting on auth endpoints
- Input validation
- Least privilege DB access
10) Recommended implementation sequence
A good order:
- Set up DB and app hosting
- Implement auth
- Create user/account tables
- Add protected routes
- Add Stripe products/prices
- Implement billing checkout
- Add Stripe webhook processing
- Gate premium features
- Add customer portal
- Add emails and onboarding
- Add analytics/logging
- Add roles, teams, and seat billing if needed
11) Minimal SaaS data model
A common schema:
users
idemailnameauth_provider_idcreated_at
organizations
idnameowner_user_idcreated_at
organization_members
idorganization_iduser_idrole
subscriptions
idorganization_idoruser_idstripe_customer_idstripe_subscription_idstatusplancurrent_period_endcancel_at_period_end
12) Best “fastest to launch” combo
If you want the simplest production-ready setup:
- Frontend: Next.js
- Auth: Clerk
- Billing: Stripe Checkout
- Database: Supabase Postgres or Neon
- Hosting: Vercel
- Email: Resend
This avoids a lot of custom auth complexity and gets you to launch quickly.
13) Common mistakes to avoid
- Building custom auth too early
- Not using webhooks for billing state
- Not storing subscription state locally
- Forgetting org/team billing
- Not verifying Stripe webhook signatures
- No plan gating in backend
- Hardcoding premium access only in frontend
- No retry handling for failed payments
- No customer portal link
14) If you want the simplest architecture
SaaS stack starter:
- Next.js + Vercel
- Clerk auth
- Stripe subscriptions
- Neon Postgres
- Resend email
Flow:
- User signs up with Clerk
- User creates workspace/org
- User subscribes via Stripe Checkout
- Stripe webhook updates subscription table
- App checks subscription status on every protected premium action
If you want, I can also give you:
- a recommended SaaS architecture diagram
- a step-by-step implementation checklist
- a Next.js + Clerk + Stripe starter code structure