Login sends users to localhost or the preview URL after publishing
Updated · by Zane
Why users end up on localhost
Several login flows leave your app and come back. The confirmation email after sign-up, a magic link, a password reset link and the return trip from Google all work this way. Each one needs an address to return the user to.
Supabase decides that address from two settings in your project. Site URL is the default destination. Redirect URLs is an allow list of other addresses your code may ask for. If your code passes emailRedirectTo or redirectTo and that address is not on the allow list, it is ignored and the user lands on Site URL instead.
AI app builders create the project while you are on a preview address or on localhost. The usual cause of this bug is that both settings never moved on to your real domain when you published.
Step 1: look at where the link actually goes
Do not guess. Sign up in a private window with a new email address and open the email. Copy the link from the button instead of clicking it. It usually points at your Supabase project first and carries the final destination at the end, after redirect_to=. Read that part. Then click the link and note where you land.
- The destination is localhost or your preview address: the Supabase settings are the problem. Go to the next step.
- The destination is correct but the app sends you back to the login page: that is a session problem. Skip to the login loop section.
- The error is on a Google page: skip to the Google section.
Step 2: fix Site URL and Redirect URLs
- Open the Supabase Dashboard, then Authentication, then URL Configuration.
- Set Site URL to your real domain. Use https and no extra path, for example
https://yourdomain.com. - In Redirect URLs, add your real domain. A wildcard works, for example
https://yourdomain.com/**. - If you still want the preview address to work, add it here too.
- Save.
The change does not touch emails that were already sent. Their links were built with the old address and will stay that way. Trigger a new email every time you retest.
Step 3: stop typing localhost into the code
Search your project for localhost and for your preview domain. If you find it inside a sign-up, sign-in or reset call, replace it. Build the address from where the app is running, so the same code works in preview and in production:
// Build the redirect from where the app is actually running
const { error } = await supabase.auth.signUp({
email,
password,
options: { emailRedirectTo: `${window.location.origin}/` },
});
// OAuth sign-in works the same way
await supabase.auth.signInWithOAuth({
provider: "google",
options: { redirectTo: `${window.location.origin}/dashboard` },
});This only works if the resulting address is allowed in Redirect URLs. That is why step 2 matters even when the code is right.
Google login: the extra step
Google login has a second layer, because Google also checks where it may send the user. If you see this on a Google page, the cause is in Google Cloud Console:
Error 400: redirect_uri_mismatchThe OAuth client in Google Cloud Console needs the right values in two fields. Authorized redirect URIs should contain the Supabase callback address, not your app's address:
https://<project-ref>.supabase.co/auth/v1/callbackAuthorized JavaScript origins should contain your real domain. Your project reference is in the Supabase project settings. Google can take a few minutes to apply a change, so wait before retesting.
When login works but you get kicked back to the login page
This loop has two usual causes.
The page decides too early
When the user returns from an email link or from Google, the session takes a moment to load. If a protected page checks for a user in that moment, it sees nothing, concludes the user is logged out and redirects to login. The fix is to wait for the session before deciding:
// Wait for the session before deciding the user is logged out
const [loading, setLoading] = useState(true);
const [session, setSession] = useState<Session | null>(null);
useEffect(() => {
supabase.auth.getSession().then(({ data }) => {
setSession(data.session);
setLoading(false);
});
const { data: sub } = supabase.auth.onAuthStateChange((_event, s) => setSession(s));
return () => sub.subscription.unsubscribe();
}, []);
if (loading) return null; // do not redirect to /login yetTwo domains in play
If some links use www.yourdomain.com and others use yourdomain.com, the session is stored under one and invisible to the other. Pick one, set it as Site URL, and make the other redirect to it at your hosting provider.
Test the fix
- Open your real domain in a private window.
- Sign up with a new email and open the confirmation link from the new email.
- Check that you land on your real domain and stay logged in.
- Log out, log in, and request a password reset. Open that new link as well.
- Run Google login separately, from start to finish.
If the screen goes blank instead of showing an error, that is a different problem. See blank white page after publishing.
Related
Working login gets people into your app, and what they can read once inside is set by row level security. Spend two minutes on checking whether your Supabase data is public.
FAQ
Can I keep the preview URL working too?
Yes. Site URL holds one value, so set it to your real domain. Then add the preview address to Redirect URLs as well. Links that ask to return to the preview will be allowed, and everything else falls back to the real domain.
Why does Google login work in preview but not on my domain?
The usual cause is that your domain is missing from the Google OAuth client's Authorized JavaScript origins, or the redirect URI there is your app's address instead of the Supabase callback. Both are fixed in Google Cloud Console, not in your app.
Users say the confirmation email link is broken.
Links in emails sent before you changed Site URL still carry the old address. They will not update. Ask the user to request a new email, and test with a fresh one yourself.
Checklist
Still stuck?
Send me the app link and what's broken. The first look is free and you get a fixed quote before I touch anything.
Related guides
- Bolt.new Stripe webhook not working after deploying to Netlify (404 or no upgrade)
- Lovable custom domain with Cloudflare: fix "too many redirects" (ERR_TOO_MANY_REDIRECTS)
- Lovable custom domain not working: stuck pending, no SSL, or www fails
- Lovable email verification not working: signup confirmation emails not arriving
- Lovable Google sign in showing redirect_uri_mismatch or an unverified app warning
- Base44 custom domain stuck on Pending verification
- Base44 payment succeeded, but the customer was not upgraded
- Lovable Stripe checkout: "Edge Function returned a non-2xx status code"
- Lovable password reset link not working or sending users to the wrong page
- Replit custom domain not working: stuck on Verifying or showing a blank page
- Is your Supabase data public? A two-minute RLS check
- Blank white page after publishing your AI-built app
- “No such price” when going live: Stripe test and live mode mixed up
- Stripe payment succeeded but the user is still on the Free plan
- Subscription cancelled in Stripe but the user still has the paid plan
- Stripe webhook signature verification failed (400): causes and fix
- Supabase update returns no error but nothing changes