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.

Supabase sends the user to the address your code asks for only if it is on the Redirect URLs list. Otherwise the request is ignored and the user lands on Site URL, which stays on localhost or a preview address if it never moved to your real domain.

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

  1. Open the Supabase Dashboard, then Authentication, then URL Configuration.
  2. Set Site URL to your real domain. Use https and no extra path, for example https://yourdomain.com.
  3. In Redirect URLs, add your real domain. A wildcard works, for example https://yourdomain.com/**.
  4. If you still want the preview address to work, add it here too.
  5. 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:

Redirect addresses built from the current origin
// 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:

Google error
Error 400: redirect_uri_mismatch

The 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:

Authorized redirect URI
https://<project-ref>.supabase.co/auth/v1/callback

Authorized 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
// 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 yet

Two 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

  1. Open your real domain in a private window.
  2. Sign up with a new email and open the confirmation link from the new email.
  3. Check that you land on your real domain and stay logged in.
  4. Log out, log in, and request a password reset. Open that new link as well.
  5. 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