Why Social Logins and Magic Links Fail When You Switch to a Custom Domain
Everything worked on Vercel preview URLs. The moment we attached app.domain.com, Google threw Error 400 redirect_uri_mismatch. Here is the URL configuration fix.
01Run this check right now
Diagnostic TestThe OAuth Redirect URL Inspection
Check your authentication provider settings in Supabase under Authentication > URL Configuration. If the Site URL is still set to localhost:3000 or your Vercel preview URL, magic links and social logins will redirect users away from your custom domain.
curl -I "https://accounts.google.com/o/oauth2/v2/auth?redirect_uri=https://yourdomain.com/auth/callback"Findings from the Field
- /01I reproduced a launch-day failure where switching to a custom domain broke 100% of Google logins with Error 400: redirect_uri_mismatch.
- /02OAuth providers demand strict exact-match whitelist verification to prevent authorization code theft.
- /03Supabase defaults Site URL to whatever was configured on day one, routing password resets and magic links to stale preview URLs.
- /04How to configure wildcard redirect rules that support both temporary staging previews and primary custom domains.
- /05Why dynamic window.location.origin in client auth calls prevents hardcoded domain drift.
The launch day OAuth 400 error
A team was preparing for their public beta launch. For two weeks, their team and initial testers logged in using Google OAuth and email magic links on their Vercel preview URL (`https://myapp-preview-team.vercel.app`). It worked without a hitch.
On launch morning, they bought a custom domain, configured DNS to point `app.mystartup.com` to Vercel, and clicked 'Sign in with Google' on the live site. Instead of the Google account selector, they hit Google's dreaded white screen: `Error 400: redirect_uri_mismatch`.
To make matters worse, users trying to log in with magic links received emails where the sign-in button redirected them back to an old preview URL from ten days ago. Authentication was completely offline.
How OAuth callback whitelisting works
OAuth 2.0 is designed to prevent authorization code interception. When a user clicks 'Log in with Google', your application redirects the browser to Google with a query parameter like `redirect_uri=https://project.supabase.co/auth/v1/callback`.
Google compares this requested URI against the exact whitelist configured in your Google Cloud Console. If the URI differs by a single letter, protocol (`http` vs `https`), or trailing slash, Google immediately terminates the flow.
Google does not know that `app.mystartup.com` belongs to you until you explicitly register it in the developer console.
The postal forwarding analogy
Think of it like moving to a new apartment. You print shiny new business cards with your new street address and hand them out to colleagues. But you forget to file a change-of-address form with the post office.
When an important certified letter is dispatched, the courier drives to your old college dorm room because that is the destination on file with the postal authority. If nobody lives at your old dorm, the letter bounces.
Step 1: Update Supabase site URL and redirect wildcards
In your Supabase project dashboard, open **Authentication** and navigate to **URL Configuration**.
Set **Site URL** to your primary production domain: `https://app.mystartup.com`.
Next, update **Redirect URLs** to include wildcard allowances so both local testing and production custom domains work simultaneously:
https://app.mystartup.com/**
https://mystartup.com/**
https://*-yourteam.vercel.app/**
http://localhost:3000/**
http://localhost:5173/**Step 2: Register callback in Google Cloud Console
Open the Google Cloud Console, navigate to APIs & Services > Credentials, and open your OAuth 2.0 Client ID.
Under **Authorized redirect URIs**, add your Supabase project callback endpoint:
Under **Authorized JavaScript origins**, add your new custom domain: `https://app.mystartup.com`.
https://<YOUR_PROJECT_ID>.supabase.co/auth/v1/callbackStep 3: Dynamic client redirect handling
In your frontend authentication code, avoid hardcoding static domain strings. Construct the destination URL dynamically from `window.location.origin`:
import { supabase } from "@/lib/supabase";
export async function loginWithGoogle() {
// Uses whatever domain the user is currently looking at
const origin = window.location.origin;
const { error } = await supabase.auth.signInWithOAuth({
provider: "google",
options: {
redirectTo: `${origin}/auth/callback`,
},
});
if (error) {
console.error("Sign in failed:", error.message);
}
}Reflections: checking email templates
After saving the URL configuration in Supabase and updating Google Cloud Console, I tested social sign-in on `app.mystartup.com`. The Google consent screen loaded in 90ms, and returning to `/auth/callback` established the session immediately.
One last check: verify your email templates under Authentication > Email Templates in Supabase. Make sure the action button uses `{{ .ConfirmationURL }}`, which automatically adopts the newly configured Site URL.
Now social login is rock solid. Time for a quick break and back to work, because yolo.
Production Hardening & Audits
Stuck on prompt regressions, leaked keys, or database scaling in an AI-built app? Explore our platform-specific teardowns:
Diagnostic & Rescue
1 business day replyThink your prototype is vulnerable or hitting scale limits?
Send us a note with your repository, Supabase link, or project details. A senior engineer will review your policies and reply within one business day.
