Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Supabase sign in redirects to localhost or an internal address in production

ErrorRedirected to http://localhost:3000 after Google sign in on the live site

Short answer

The callback route builds its redirect from new URL(request.url).origin. Behind a proxy or load balancer that origin is the internal host, so users land on the wrong address. Redirect to https://x-forwarded-host in production, and set Supabase's Site URL and Redirect URLs list to your real domain.

  • Applies to Next.js 15, @supabase/ssr, Supabase Auth, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log the origin and the forwarded host inside app/auth/callback/route.ts and open the sign in flow on the live domain. The two values tell you which cause applies.

    const { origin } = new URL(request.url)
    console.log('origin', origin, 'forwarded', request.headers.get('x-forwarded-host'))
  2. 2Match what you see, then jump to its fix

    1. 1The log shows an internal origin while x-forwarded-host holds your public domainThe callback uses request.url origin behind a proxy or load balancerFix
    2. 2Confirmation and reset emails also link to http://localhost:3000Supabase Site URL is still the default localhost valueFix

What does the error look like?

  • Redirected to http://localhost:3000 after Google sign in on the live site

Cause 1 of 2

The callback uses request.url origin behind a proxy or load balancer

1Check

Supabase's example reads origin from request.url. Behind a load balancer that is not the address the visitor used. The header x-forwarded-host carries the original host.

# read the log line from the first step in your server output

2Fix

In production, use x-forwarded-host when it exists; keep origin for local development.

const forwardedHost = request.headers.get('x-forwarded-host')
const isLocalEnv = process.env.NODE_ENV === 'development'
if (isLocalEnv) {
  return NextResponse.redirect(`${origin}${next}`)
} else if (forwardedHost) {
  return NextResponse.redirect(`https://${forwardedHost}${next}`)
} else {
  return NextResponse.redirect(`${origin}${next}`)
}

3Verify

Sign in on the live domain and check the address after the callback.

# complete a sign in in a private window

Confirmed by supabase.com

Did this fix it?

Cause 2 of 2

Supabase Site URL is still the default localhost value

1Check

The Site URL is the default redirect when no redirectTo is given. Its default is http://localhost:3000, and the docs say to change it to your production URL.

# Supabase dashboard: Authentication > URL Configuration

2Fix

Set Site URL to your production URL, and add the redirect URLs you use. Add the localhost wildcard for development.

Site URL:       https://example.com
Redirect URLs:  https://example.com/**
                http://localhost:3000/**

3Verify

Request a password reset email and open the link.

# the link should start with your production domain

Confirmed by supabase.com

Did this fix it?

How do I stop it coming back?

  • Pass an explicit redirectTo built from your public URL when calling sign in methods.
  • Keep separate Supabase projects or URL lists for production and preview, and write the exact paths in the allow list.
  • Test the callback on the live domain after any proxy or CDN change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.