Why does login work in my Lovable or Bolt preview but break after I deploy?
Run a free check: FlowWatch signs up, logs in and runs a Stripe test checkout on your app, then scans for exposed keys. You get a shareable report. Run the free check at flowwatch.org
Login works in the builder's preview. You publish to your own domain, and suddenly sign-in loops back to the login page, a magic link opens the preview site, Google sign-in shows a "redirect_uri_mismatch" error, or the session disappears on refresh. Your code usually did not change. What changed is the host name, the environment, and how the browser handles cookies on the new domain.
Below are the usual causes, in the order worth checking. The examples use Supabase Auth because Lovable and Bolt apps commonly use it, but the same ideas apply to other auth providers.
1. Site URL still points at the preview host
In Supabase, go to Authentication → URL Configuration. The Site URL is the default place users are sent after confirming an email or clicking a magic link when no valid redirect is supplied.
- If it still says something like
https://your-project.lovable.appor a Bolt / StackBlitz preview URL, confirmation and magic-link emails will send people to the preview, not your production site. - Set Site URL to your production origin, for example
https://yourapp.com(no trailing path).
2. Production URL missing from the redirect allowlist
On the same page, Redirect URLs (additional redirect URLs) is an allowlist. If your code calls signInWithOtp, signInWithOAuth or resetPasswordForEmail with redirectTo: window.location.origin + '/auth/callback' and that URL is not on the list, Supabase falls back to the Site URL, which can look like "login sends me to the wrong site."
- Add every origin you really use:
https://yourapp.com/**,https://www.yourapp.com/**, and the preview host if you still want preview login to work. wwwand non-wwware different origins. So arehttpandhttps.- Keep wildcards narrow. Do not allowlist hosts you do not control.
3. OAuth provider redirect URIs and authorized origins
For Google, GitHub and similar providers there are two layers:
- In the provider's console (for example Google Cloud → Credentials → your OAuth client), the authorized redirect URI should be your Supabase callback (
https://<project-ref>.supabase.co/auth/v1/callback), or your custom auth domain if you set one up. If you added authorized JavaScript origins, include your production domain. - In Supabase, the app's return URL after the provider must be on the Redirect URLs allowlist (step 2).
A redirect_uri_mismatch error comes from the provider (layer 1). Landing back on the preview host or the login page usually points to layer 2 or the Site URL.
4. Hard-coded preview URLs in the code
AI-generated code sometimes hard-codes the host it was built on. Search the repo for:
lovable.app,lovableproject.com,bolt.new,stackblitz,webcontainer,localhostredirectTo:andemailRedirectTo:values
Replace hard-coded hosts with window.location.origin (in the browser) or an environment variable such as VITE_SITE_URL / NEXT_PUBLIC_SITE_URL, and make sure that value is allowlisted.
5. Environment variables missing or different in production
The preview environment may inject variables that your host (Vercel, Netlify, Cloudflare Pages, etc.) does not have.
- Check that
VITE_SUPABASE_URL/VITE_SUPABASE_ANON_KEY(or theNEXT_PUBLIC_equivalents) are set in the production environment on your host, not only in preview or locally. - Vite and Next.js bake public variables in at build time. After adding or changing one, redeploy. Editing the dashboard value alone does not change an already-built bundle.
- Confirm prod points at the same Supabase project you configured in steps 1–3. A login that "works in preview" against one project and fails against another is common when there are separate dev and prod projects.
- Only the anon / publishable key belongs in the browser. If you find a
service_rolekey in front-end variables while you are here, remove it and rotate it.
6. Confirmation and reset emails using the preview host
Email templates under Authentication → Email Templates use {{ .SiteURL }} and {{ .ConfirmationURL }}. If Site URL is wrong (step 1), or a template hard-codes a preview link, every confirmation email points at the wrong site. Send yourself a fresh signup email after fixing step 1. Links in older emails will not change.
7. Cookie, SameSite and domain differences
If you use server-side auth (for example @supabase/ssr with Next.js middleware), sessions live in cookies, and cookies behave differently on a new domain:
- A cookie set with
Domain=preview-hostor by a different subdomain will not be sent toyourapp.com. SameSite=NonerequiresSecure(HTTPS). Mixedhttp/httpssetups drop the cookie silently.- Bouncing between
wwwand the bare domain during login can lose the cookie. Pick one canonical host and redirect the other to it before auth starts. - Embedded previews run inside an iframe, and once deployed the app runs top-level, so cookie and storage behavior can differ in both directions. Test on the real domain, not only in the builder.
Symptoms: login appears to succeed, then the next request or a refresh shows you as logged out.
8. SPA routing for the callback path
If your auth callback is a client-side route like /auth/callback and your host does not rewrite unknown paths to index.html, the callback returns a 404 in production even though it worked in preview. Add the SPA fallback rewrite for your host (for example a vercel.json rewrite, or a Netlify _redirects rule like /* /index.html 200).
A quick verification pass after any fix
- Open your production URL in a private window.
- Sign up with a new address and click the confirmation email. Check the link points at your production host.
- Log out, log back in with password, then with magic link or OAuth if you offer them.
- Refresh on a logged-in page and confirm the session survives.
- Repeat on
wwwvs bare domain if both resolve.
Re-run this pass after any deploy that touches auth, domains or environment variables.
Automating the related checks
FlowWatch runs a real signup and login (plus a Stripe test-mode checkout) against your production URL on a schedule, and emails you if the flow breaks and again when it recovers. It only runs test-mode checkouts and refuses to run if it finds live keys. It will not fix your Supabase URL configuration for you, and it does not replace the manual pass above, but it tells you when a later deploy breaks login again. Start with the free check: flowwatch.org.
Contact: hello@flowwatch.org