Skip to content

All posts

Best AI Prompt to Fix "redirect_uri_mismatch" and Login Redirecting to Localhost (Supabase Auth)

Your login breaks in production because three places still hold your old localhost or preview URL. Set the Site URL in Supabase to your live domain, add your live domain to Supabase's Redirect URLs, and make sure Google's Authorized redirect URI

4 min read

Quick answer: Your login breaks in production because three places still hold your old localhost or preview URL. Set the Site URL in Supabase to your live domain, add your live domain to Supabase's Redirect URLs, and make sure Google's Authorized redirect URI is exactly https://YOUR-PROJECT.supabase.co/auth/v1/callback.

This covers all of these symptoms:

  • Google shows Error 400: redirect_uri_mismatch
  • Supabase returns Invalid redirect URL
  • After login, users land on http://localhost:3000 and see "This site can't be reached"
  • Email confirmation and password reset links open localhost
  • Login works in the Lovable or Bolt preview but not on your own domain

Why it happens

OAuth login is a chain of redirects: your app, then Google, then Supabase, then back to your app. Each hop only accepts URLs on its allow-list. Your AI tool set those lists up for the preview URL. When you moved to a real domain, the lists did not follow.

Fix 1: Supabase Site URL (fixes the localhost redirect)

In Supabase, go to Authentication, URL Configuration.

  • Site URL: set it to https://yourdomain.com

Supabase falls back to the Site URL whenever the redirect your app asks for is not on the allow-list. If Site URL still says http://localhost:3000, that is where your users end up. Email confirmation links use it too.

Fix 2: Supabase Redirect URLs (fixes "Invalid redirect URL")

On the same page, under Redirect URLs, add:

https://yourdomain.com/**
https://www.yourdomain.com/**
http://localhost:3000/**

Add your preview URL as well if you still test there. Keep localhost so local development continues to work.

Fix 3: Google Cloud Console (fixes redirect_uri_mismatch)

In Google Cloud Console, go to APIs and Services, Credentials, and open your OAuth 2.0 Client ID.

  • Authorized redirect URIs: https://YOUR-PROJECT.supabase.co/auth/v1/callback
  • Authorized JavaScript origins: https://yourdomain.com

The redirect URI must point to Supabase, not to your app. This is the mistake most people make. Copy the exact value from Supabase under Authentication, Providers, Google, Callback URL. Google can take a few minutes to apply the change.

It must match character for character. These all count as different URLs:

Difference

Example

http vs https

http://yourdomain.com vs https://yourdomain.com

www vs no www

www.yourdomain.com vs yourdomain.com

Trailing slash

/auth/v1/callback/ vs /auth/v1/callback

Wrong project

An old Supabase project ref in the URL

Fix 4: Remove hardcoded URLs from your code

Search your codebase for localhost and for your preview domain. Replace any hardcoded redirect with the current origin:

await supabase.auth.signInWithOAuth({
  provider: 'google',
  options: {
    redirectTo: `${window.location.origin}/auth/callback`,
  },
});

This works on localhost, preview and production without changes, as long as each origin is listed in Fix 2.

Test it properly

Open a private browser window, go to your live domain and sign in with Google. Then request a password reset and click the email link. Both should end on your live domain, logged in.

The prompt to give your AI tool

Search the whole codebase for hardcoded localhost URLs and preview domains in auth code. Replace them with window.location.origin. Show me every signInWithOAuth, signUp and resetPasswordForEmail call and the redirectTo or emailRedirectTo value each one uses. Do not change anything else.

FAQ

Login works but the user is logged out after the redirect. The route in redirectTo is not exchanging the code for a session, or it is not in your Redirect URLs list so the Site URL was used instead. Check Fix 2 first.

I changed everything and still get redirect_uri_mismatch. Open the Google error page and click "error details". It shows the exact URI that was sent. Paste that value into Authorized redirect URIs.

Does this apply to GitHub, Apple or other providers? Yes. Fixes 1, 2 and 4 are identical. For Fix 3, put the same Supabase callback URL in that provider's settings.

Google shows "Access blocked: this app's request is invalid". Same cause. It is how Google words redirect_uri_mismatch on some screens.

Still stuck?

Rayionyx fixes broken AI-built apps. Run the free scan at rayionyx.com, or book the $199 Rescue Audit for a full written report in 24 hours.

Can we watch how you use this site?

We use Microsoft Clarity to see where visitors click and scroll, so we can fix confusing pages. It sets cookies and records your visit. Nothing loads unless you say yes.

Read the privacy notice