← All guides

Supabase Auth Redirect URLs and Password Reset Security: Stop Login Links Going to the Wrong Site

Updated 2026-11-12

Short answer: set your Supabase Site URL to your production domain (not http://localhost:3000), and keep the Redirect URLs allow list to exact production paths plus tightly scoped preview patterns. Avoid broad wildcards like https://** or https://*.vercel.app/**, since anyone can create a page on a shared hosting domain. In your own auth callback route, only accept a next redirect that is a relative path starting with a single /. For password resets, call resetPasswordForEmail with a redirectTo on your domain, exchange the code on the server, and only then let the user call updateUser({ password }).

AI builders like Lovable, Bolt and v0 usually get login working by adding whatever URL makes the error go away. That's how projects end up with http://localhost:3000 as the Site URL in production, or a ** wildcard that matches almost anything. Login links and OAuth codes are bearer credentials: whoever receives them can often sign in as that user.

How Supabase decides where to send users

Supabase Auth uses two settings under Authentication > URL Configuration:

The allow list supports glob wildcards. Per Supabase's docs, * matches any run of characters except the separators . and /, ** matches anything including separators, and ? matches one non-separator character. Supabase recommends exact redirect paths for production and saving ** for local development and previews.

Mistake 1: Site URL left on localhost or a preview domain

If the Site URL is still http://localhost:3000, confirmation and reset emails that rely on the default send real users to their own machine, where the link fails. Users retry, request more emails, and support tickets pile up. If the Site URL is an old preview or a domain you've let expire, links can land on a host you no longer control.

Fix: set the Site URL to https://yourapp.com and update your email templates. If you pass redirectTo, Supabase notes you may need {{ .RedirectTo }} instead of {{ .SiteURL }} in templates.

Mistake 2: Wildcards that match other people's sites

These entries are risky in production:

https://**
https://*.vercel.app/**
https://*.netlify.app/**
https://*.lovable.app/**

Shared hosting domains give out subdomains to anyone. If your allow list accepts https://*.vercel.app/**, an attacker can deploy https://evil-demo.vercel.app, then start a login with redirectTo pointed there. Depending on your flow, the magic link token or OAuth code ends up on their page.

Safer patterns:

https://yourapp.com/auth/callback
https://yourapp.com/auth/confirm
https://www.yourapp.com/auth/callback
http://localhost:3000/**
https://*-your-team-slug.vercel.app/**

The last line scopes Vercel previews to your team's slug, which is the pattern Supabase documents for preview URLs. For Netlify, Supabase documents https://**--your_org.netlify.app/**. Better still, use a separate Supabase project or preview branch for previews so production auth never trusts preview domains.

Also turn on PKCE (the default in @supabase/ssr) so OAuth and magic link flows return a short-lived code that must be exchanged with a verifier stored on the original device. That makes a stolen redirect far less useful, but it is a second layer, not a reason to keep loose wildcards.

Mistake 3: An open redirect in your own callback route

Even with a perfect allow list, your app can reintroduce the problem. A common Next.js callback looks like this:

// app/auth/callback/route.ts (unsafe)
export async function GET(request: Request) {
  const { searchParams, origin } = new URL(request.url);
  const code = searchParams.get("code");
  const next = searchParams.get("next") ?? "/";
  if (code) {
    const supabase = await createClient();
    await supabase.auth.exchangeCodeForSession(code);
  }
  return NextResponse.redirect(`${origin}${next}`);
}

Prefixing with origin looks safe, but string concatenation is fragile. A value like @evil.com turns https://yourapp.com into https://[email protected], which browsers treat as a visit to evil.com. Callbacks that redirect to next without the origin prefix are worse: next=//evil.com or next=https://evil.com goes straight off-site. Attackers use these links for phishing because they start on your real domain.

Validate next and build the URL with the URL constructor:

function safeNext(raw: string | null): string {
  if (!raw) return "/";
  // Must be a single-slash relative path. No "//", no backslashes, no schemes.
  if (!raw.startsWith("/") || raw.startsWith("//") || raw.includes("\\")) return "/";
  return raw;
}

export async function GET(request: Request) {
  const url = new URL(request.url);
  const code = url.searchParams.get("code");
  const next = safeNext(url.searchParams.get("next"));

  if (code) {
    const supabase = await createClient();
    const { error } = await supabase.auth.exchangeCodeForSession(code);
    if (!error) {
      const dest = new URL(next, url.origin);
      if (dest.origin === url.origin) return NextResponse.redirect(dest);
    }
  }
  return NextResponse.redirect(new URL("/login?error=auth", url.origin));
}

Use the same check anywhere you accept returnTo, redirect or callbackUrl, including your login page and middleware.

Building a safe password reset flow

Step 1: Request the email

const { error } = await supabase.auth.resetPasswordForEmail(email, {
  redirectTo: "https://yourapp.com/auth/callback?next=/account/update-password",
});
// Show the same message whether or not the email exists.
setMessage("If that email has an account, a reset link is on its way.");

Don't reveal whether an address is registered. Showing "no account found" lets anyone check which emails use your app. Supabase also rate limits auth emails, so add your own CAPTCHA (for example Cloudflare Turnstile, which Supabase Auth supports) if bots start hammering the form.

Step 2: Exchange the code on your server

The link lands on your callback route, which runs exchangeCodeForSession(code) and redirects to /account/update-password using the safe next check above. The user now has a recovery session on that device.

Step 3: Update the password, with a session check

// app/account/update-password/actions.ts
"use server";
export async function updatePassword(formData: FormData) {
  const supabase = await createClient();
  const { data: { user } } = await supabase.auth.getUser();
  if (!user) throw new Error("Session expired, request a new reset link.");

  const password = String(formData.get("password") ?? "");
  if (password.length < 12) throw new Error("Use at least 12 characters.");

  const { error } = await supabase.auth.updateUser({ password });
  if (error) throw error;
}

Use getUser() rather than trusting a cookie value, as explained in Supabase getSession vs getUser on the server. Never accept a user ID or email from the form to decide whose password changes. And never "reset" passwords through an Edge Function that calls the admin API with the service role key based on an email in the request body; that lets anyone change anyone's password.

Step 4: Tidy up

Quick audit checklist

  1. Site URL is your production domain over HTTPS.
  2. No https://** or bare *.vercel.app, *.netlify.app or similar shared-domain wildcards in production Redirect URLs.
  3. Email templates use the right variable ({{ .SiteURL }} or {{ .RedirectTo }}).
  4. Your callback rejects next=//evil.com, [email protected] and next=https://evil.com. Test all three.
  5. The reset form gives the same response for known and unknown emails.
  6. The update-password action checks the session with getUser() and uses no IDs from the request.
  7. No admin password changes based on client input.

Related guides

Get an automated check

PreflightPR reviews AI-built apps for unsafe redirects, service role keys in client code, API routes without auth checks, missing RLS and leaked keys. Run a free scan on a public repo for a launch score and a prioritized fix list, or install the PreflightPR GitHub App to check every pull request before it merges.

Check your repo in about a minute

Paste a public GitHub URL and get a free launch score.

Run a free scan

More guides