Login dies on custom domain? Check cookies
You signed in on the builder preview and it stuck. Then you connected a custom domain, opened the live address, signed in again, and landed straight back on the login page. Or the dashboard flashes once and you are signed out. Your login code did not change. The session never sticks on the new host. Sites built with Lovable, Bolt, Replit, v0 or Cursor hit this a lot the week a real domain goes live, because the preview URL and the custom domain are two different cookie hosts.
The symptom
- Sign-in works on the
*.lovable.app, Bolt, Replit or Vercel preview URL, and fails onhttps://yoursite.com. - After login the logged-in UI flashes, then you bounce to
/loginon the next click or refresh. - Incognito stays signed out while your everyday browser still holds an old preview session.
- Network shows
Set-Cookieon login, but the next API request sends no session cookie.
The cause
Browsers keep login state in cookies (or in tokens that depend on them). A cookie is bound to a host, a path, a Secure flag and a SameSite policy. Moving from preview to a custom domain can break any of those:
- Wrong Domain. A cookie set for the preview host never applies to your custom domain.
- Missing Secure. Live sites are HTTPS. Auth cookies without
Securecan be dropped or ignored. - SameSite too strict.
SameSite=Strictcan block the cookie after an OAuth redirect. True cross-site API calls needSameSite=None; Secureplus CORS credentials. - UI and API on different hosts. Live may put the UI on
yoursite.comand the API onapi.yoursite.comor a Supabase URL. Cookies on one host stay invisible to the other unless Domain and CORS are set on purpose. - www vs bare domain. A session on
www.yoursite.comdoes not apply toyoursite.comwithout one canonical host. - Stale localStorage. JWTs in localStorage are per origin, so preview login proves nothing about live.
Step-by-step diagnosis
- Reproduce on the live host only in a private window. Skip the preview URL for this bug.
- Inspect Set-Cookie on the login response:
Domain,Path,Secure,HttpOnly,SameSite, expiry. - Inspect the next API call. If Set-Cookie succeeded but Cookie is empty, the browser rejected the cookie for this host.
- Compare auth allow lists in Supabase, Clerk, Auth.js or Firebase: they must list the live domain, not only preview.
- Check www and apex. If only one keeps the session, pick a canonical host and redirect first.
- Diff preview vs production env for cookie domain,
SITE_URL, auth public URLs and hard-coded preview hosts.
Fixes per builder or host
Supabase Auth (common with Lovable and Bolt)
Set the live Site URL and every live redirect URL. Use the official client with the production project URL and anon key. If the browser talks to *.supabase.co while your UI is on yoursite.com, follow your Supabase version's cross-origin session guidance and send credentials where required. Remove any leftover preview project URL from production env vars.
Lovable and Bolt
After you attach a custom domain, re-test login there in a private window. Update auth settings that still list only the builder preview host. If setup wrote a preview Site URL into Supabase or Clerk, replace it with the live URL and redeploy so client env vars match.
Auth.js / NextAuth on Vercel (common with v0 and Cursor)
Set AUTH_URL / NEXTAUTH_URL to the live HTTPS origin and use secure: true cookies in production. SameSite Lax is usually enough when UI and auth share one host. If you split hosts, set cookie Domain on the parent (for example .yoursite.com) and use SameSite=None; Secure only for true cross-site calls. Redeploy after env changes; preview deploys do not prove production cookies.
Clerk, Firebase and similar
Add the custom domain to allowed origins / authorized domains. For Clerk, follow their custom-domain docs when sign-in UI and the product live on different hosts. For Firebase, a missing authorized domain lets sign-in complete while the session never attaches.
Replit Deployments
Allow the deployment URL and custom domain, not only the workspace URL. Confirm HTTPS end-to-end and that session middleware sets Secure cookies in production.
How to verify
- Private window on the canonical live URL: sign in, refresh, open another auth-only page. You should stay signed in.
- Sign out and in again; Set-Cookie should show Secure and the Domain you expect.
- Repeat on phone data so an old preview cookie cannot hide the bug.
- If both www and apex resolve, confirm the redirect to one host before login.
Prevention
- Pick one live origin early and put it in every auth allow list before you share links.
- Redirect www and apex to a single host so sessions are not split.
- Keep preview and production auth settings separate; never leave a preview Site URL in production.
- After every domain or auth-provider change, run a private-window login on the live host before launch.
