DEV Community

Cover image for Supabase auth emails link to localhost? Fix your redirect URLs
Eric Culus
Eric Culus

Posted on Originally published at rescuemysites.com

Supabase auth emails link to localhost? Fix your redirect URLs

There's no news in our lane today, so here is a fix for one of the most common launch bugs we see in Lovable, Bolt, Replit and Cursor apps on Supabase. Sign-up works on your machine. You deploy, a real user asks for a magic link or a password reset, and the link in their inbox opens http://localhost:3000. On their machine, that's nothing.

The cause is almost always one dashboard setting that never left its development value. The fix takes about ten minutes, and you can do it without a redeploy for the dashboard part.

Why the link points at localhost

Supabase Auth builds every email link from two settings. The Site URL is, in Supabase's words, the "default redirect URL when no redirectTo is specified in the code". The Redirect URLs list is an allow list: any URL your code passes as redirectTo or emailRedirectTo should match an entry in it. Supabase's own docs tell you to change the Site URL "from http://localhost:3000 to your production URL", and that step is easy to miss when an AI builder wired up auth for you while you were still on localhost.

So two things go wrong. Either the code passes no redirect at all and Supabase falls back to the localhost Site URL, or the code passes a production URL that isn't on the allow list.

The fix, step by step

  1. Set the Site URL. In the Supabase dashboard, open your project, then Authentication, then URL Configuration. Set Site URL to your production origin, such as https://app.example.com.
  2. Fill the allow list. Under Redirect URLs, add the exact callback paths your app uses in production, for example https://app.example.com/auth/callback. Supabase recommends exact paths in production.
  3. Add dev and preview patterns. For local work, add http://localhost:3000/**. On Vercel, the docs suggest a pattern like https://*-your-team-slug.vercel.app/** for preview deploys. Netlify previews follow the same idea with their own subdomain format. Remember that * stops at . and /, while ** matches anything.
  4. Pass the redirect in code. Search your codebase for the auth calls and make the target explicit: emailRedirectTo in the options of signInWithOtp and signUp, redirectTo in the options of signInWithOAuth, and redirectTo in resetPasswordForEmail(email, { redirectTo }), where the options object is the second argument rather than nested under options. Build that URL from an environment variable such as NEXT_PUBLIC_SITE_URL, set to your production origin, rather than a hard-coded string. A generated app often has http://localhost:3000 written straight into one of these calls.
  5. Check the email templates. Under Authentication, look at the confirm, magic link and reset templates. Supabase's docs note that when you use redirectTo, you may need to replace {{ .SiteURL }} with {{ .RedirectTo }}, otherwise the link ignores the target you passed.
  6. Test from a real inbox. Deploy, then request a magic link and a password reset for an address you can read, from a private window on the production domain. Click both links. Then do the same from a preview deploy if you use them.

What this doesn't fix

If the link now opens the right domain but the user lands logged out, the problem has moved further down: a missing callback route that exchanges the code for a session, cookies set on the wrong domain, or middleware that redirects before the session exists. That's the territory of our logins that drop or leak page. And if the whole app behaves differently once deployed, start with environment variables, because the same localhost value often hides in more than one place.

Sources

First published on RescueMySites.

Top comments (0)