The demo was perfect. You built it in a weekend with Lovable, Bolt, Replit, Cursor or Claude Code, showed it to friends, and it worked every time. Then real people signed up. Now a customer says they can see someone else's invoice, a payment "went through" but the account never upgraded, and the app slows to a crawl every evening.
You don't need to rebuild it yet. You need to fix an AI-generated app in production in the right order: the problems that can leak data or lose money first, the ones that only annoy people last. This post is that order, with checks you can run without reading code, the failure patterns I look for in every AI-built codebase, and a table for deciding between patching and rebuilding.
TL;DR#
- Fix in order of risk, not in order of annoyance: secrets, then who can see what, then payments, then backups, then error tracking, then deploys, then speed.
- An exposed key is only half the story. A public database key is fine when every table has access rules. Without them, anyone with the key can read and write everything.
- Most AI-built apps fail the same few ways: keys in the browser, no row-level permissions, webhooks nobody verifies, no backups, and the whole backend in one giant function.
- Patch when the data model is sound. Rebuild only the parts where it isn't, and keep the parts that work.
- Every fix gets a test that would have caught it, or the next prompt will quietly undo it.
Why it worked locally and breaks with real users#
On your laptop there is one user (you), one browser tab, sample data, and nobody trying anything clever. AI tools are very good at that world. They generate the happy path quickly, and the happy path is what a demo shows.
Production is a different place. There are hundreds of users at once, each of whom should see only their own data. Some of them will open the browser's developer tools. Payment providers send events late, twice or out of order. Databases fill up. And the code runs on a server with different environment variables than your laptop, which is how "it works on my machine" becomes a 500 error at 9 p.m.
None of this means AI-generated code is bad. It means the tool wrote what you asked for, and nobody asked for the parts that only matter once strangers use your app.
How to fix an AI-generated app in production, in order#
Here's the order I work in when someone hands me an app that's live and wobbling. It's ordered by what a mistake costs you: a leak or a double charge can end a company, while a slow page costs a few impatient users.
1. Secrets and keys#
What goes wrong: API keys for OpenAI, Stripe, email or your database end up in the frontend code that every visitor downloads. AI tools do this because it's the shortest path to a working demo. Anyone who opens developer tools can copy the key and run up your bill, or worse.
Check it without code: open your live app, press F12, go to the Sources or Network tab, and search for sk_, sk-, secret, service_role and eyJ. Also search your GitHub repo for the same strings, including old commits.
Fix: move every secret to the server (environment variables on your host), call third-party APIs from a backend function instead of the browser, and rotate every key that was ever exposed. Moving a leaked key doesn't un-leak it; only a new key does.
One nuance matters a lot here. Supabase's publishable key (the old anon key) is designed to be public: Supabase's own docs call it "safe to expose online" because Row Level Security decides what it can reach. Its secret key (the old service_role) "bypasses every Row Level Security policy you have" and must never reach a browser. Which leads to the next rung.
2. Access control: who can see what#
What goes wrong: every logged-in user can read every row, because the app filters data on the screen, not in the database. The page shows only your invoices, but the API happily returns everyone's to anyone who asks.
This is the single most common serious bug I'd expect in an AI-built app, and it isn't hypothetical. In early 2026, Wiz researchers found the Supabase database behind Moltbook open to anyone: the public key was in the frontend, as designed, but Row Level Security had never been set up, so that key gave read and write access to every table.
Check it without code: make two accounts in two different browsers. In account A, open something private (an invoice, a project, a message) and copy its URL. Paste it into account B's browser. If B can see it, you have this bug. Then ask whoever built the app: "Is Row Level Security enabled on every table, and what do the policies say?"
Fix: enable Row Level Security on every table that holds user data and write a policy for each one (Supabase's RLS guide). On Firebase, the equivalent is Security Rules; on a custom backend, it's a check in every query that the record belongs to the caller. For apps with teams or companies, the same idea applies per tenant; I go through the options in multi-tenant SaaS in PostgreSQL.
3. Payments#
What goes wrong: the app grants a subscription when the browser lands on a "success" page, instead of when Stripe confirms the payment on the server. Or it listens for Stripe's webhooks without checking they really came from Stripe, so anyone can send a fake "payment succeeded" message. Or it processes the same webhook twice and grants twice, or charges twice.
Check it without code: ask "Where do we give access after payment: on the success page or in a webhook? Do we verify the webhook signature? What happens if Stripe sends the same event twice?" If the answers are vague, this rung needs work. In your Stripe Dashboard, under Developers → Webhooks, failed deliveries are also a red flag.
Fix: grant access only from a verified webhook, store each event's ID so a duplicate does nothing, and read the subscription's real status from Stripe. I wrote the full pattern up in Stripe webhooks that never double-charge.
4. Data and backups#
What goes wrong: there are no backups, or there are backups nobody has ever restored. A bad migration, a buggy prompt-generated script or a deleted table, and the data is gone.
Check it without code: in your database provider's dashboard, find the backup settings. Note how far back you can restore and whether point-in-time recovery is on. Then ask the harder question: "When did we last restore a backup into a test database to prove it works?"
Fix: turn on automatic backups, keep a copy somewhere separate from the main database, and do one real restore. If you run your own PostgreSQL, my Postgres backup script for S3 includes the restore test most guides skip.
5. Error tracking and logs#
What goes wrong: users hit errors and you find out from an angry email, days later, with no way to see what happened.
Check it without code: ask "If a user gets an error right now, where would we see it?" If the answer is "they'd tell us," you're flying blind.
Fix: add an error tracker (Sentry or similar) to the frontend and backend, structured logs on the server, and an alert that reaches a person. On AWS, CloudWatch logs, metrics and alarms cover the server side.
6. Deploys#
What goes wrong: changes go straight from an AI prompt to production. There's no staging copy, no way to roll back, and environment variables differ between preview and production.
Fix: a staging environment with its own database, deploys through a pipeline that runs tests first, and a one-click rollback. You don't need Kubernetes for this; my zero-downtime deploy setup runs on one server.
7. Performance#
Only now, with the dangerous problems handled, is it worth chasing speed. The usual culprits in generated code are missing database indexes, a query run once per row in a loop (the "N+1" problem), and loading every record when a page shows twenty. EXPLAIN ANALYZE finds them quickly.
The "what breaks first" checklist#
You can run this list yourself in an afternoon, without opening the code. Every "no" or "I don't know" is a finding.
- I searched the live site's source in developer tools for
sk_,sk-,secretandservice_role, and found nothing - Every key that was ever in the frontend or in Git has been rotated
- Account B can't open account A's private pages by pasting the URL
- Row Level Security (or Firebase Security Rules) is on for every table with user data
- Access after payment is granted by a verified Stripe webhook, not the success page
- Sending the same Stripe event twice doesn't grant or charge twice
- Automatic backups are on, and someone has restored one in the last three months
- An error on the live site shows up somewhere we look, within minutes
- Login, signup and password reset are rate-limited
- There's a staging environment, and production changes go through it
- We can roll back the last deploy in under five minutes
The failure patterns I look for first#
When I open an AI-built codebase, these are the patterns I search for before anything else. They come up again and again because the tools optimise for "it works" in a single session:
| Pattern | What you notice | What's really happening | The fix |
|---|---|---|---|
| Exposed API keys | A surprise OpenAI or email bill | A secret key shipped in the frontend bundle | Move it server-side, rotate it |
| Missing row-level permissions | "I can see someone else's data" | The database trusts the public key; filtering happens in the UI | RLS policies or per-query ownership checks |
| No backups | Nothing, until the day you need one | No schedule, or one never tested | Automatic backups plus a real restore |
| Unverified webhooks | Paid users without access, or free users with it | Webhook handler skips signature checks and de-duplication | Verify, record event IDs, read state from Stripe |
| Everything in one function | Each new feature breaks two old ones | A 2,000-line file or one serverless function doing everything | Split by feature, add tests at the seams |
| Secrets that differ by environment | Works in preview, fails in production | Environment variables set in one place, not the other | One list of required variables, checked at startup |
Patch or rebuild?#
Founders usually ask me this first, and the honest answer is "mostly patch." A rebuild throws away the one thing your current app has proven: what users actually want. But some foundations are worth replacing. This table is how I decide, area by area:
| Area | Patch it when… | Rebuild it when… |
|---|---|---|
| Data model | Tables map to real things (users, orders, teams) and relationships are clear | Data is stored as big JSON blobs, or one table means three things |
| Auth | It uses a real provider (Supabase Auth, Clerk, Auth0, Cognito) | It's hand-rolled password storage or tokens in local storage with no expiry |
| Payments | Stripe Checkout or Billing is used, only the webhook handling is wrong | Prices and entitlements are hard-coded across the frontend |
| Backend logic | It's messy but each feature lives in its own file or function | One function does everything and nobody can change it safely |
| Frontend | It works and users like it | Rarely worth rebuilding first; it's the cheapest layer to fix later |
The usual outcome is a mix: keep the frontend, keep the data, rebuild one or two backend pieces properly, and add the tests and pipeline that were missing.
Make the fixes stick#
AI tools don't remember why code is the way it is. Ask one to "add a feature" next month, and it may happily remove the permission check you just added. Three habits stop that:
- A test for every fix. "User B gets a 403 on user A's invoice" is a two-minute test that guards the most expensive bug you had.
- A short rules file in the repo that the AI tool reads (Cursor rules,
CLAUDE.md, or similar): "Never call third-party APIs from the browser. Every table needs an RLS policy. Grant paid access only in the Stripe webhook." - Review before merge. Someone who understands the code reads every change to auth, payments and the database before it ships.
Before you announce a launch or start paid ads, run the whole SaaS launch checklist as well. It covers the rest of production readiness: monitoring, rate limits, migrations and the legal basics.
Want a second pair of eyes?#
This is the kind of work I do: rescues and audits of apps that were built fast and now need to hold up with real users. Send me your repo or app link and I'll send back the top five risks, in order, in plain language. Email mhuzaifaa.dev@gmail.com.
FAQ#
Is a vibe-coded app safe to use in production?#
It can be, but not by default. AI tools generate working features quickly and routinely skip the parts that only matter with real users: access rules, secret handling, verified payment webhooks and backups. Run a security review covering those areas before you take payments or store personal data.
Why does my AI-built app work locally but not in production?#
Usually because production differs from your laptop in ways the demo never tested: missing or different environment variables, many users at once, real payment events arriving late or twice, and a database that has grown. Check the server logs first; the error is almost always there.
Is it safe to expose my Supabase anon or publishable key?#
Yes, if Row Level Security is enabled with a policy on every table. Supabase designs the publishable key to be public. Without RLS policies, that same key gives anyone read and write access to your data. The secret or service_role key must never be in frontend code.
Should I rebuild my AI-generated app from scratch?#
Rarely all of it. Keep what's proven: the frontend users like and data that's well structured. Rebuild only the pieces with a broken foundation, such as hand-rolled authentication or one giant function holding the whole backend. A rewrite also throws away fixes you've already made.
How do I know if my API keys have been exposed?#
Open your live site, press F12 and search the loaded files for key prefixes like sk_, sk- or service_role. Search your Git history for the same strings. Unexpected usage on your OpenAI, Stripe or email dashboards is another sign. If in doubt, rotate the key; it takes minutes.
What should I fix first in an AI-generated app?#
Fix anything that can leak data or lose money first: exposed secrets, missing access rules between users, and unverified payment webhooks. Then backups and error tracking, so you can recover and see problems. Deploy pipelines and performance come last, once the risky problems are closed.
