Supabase sign in redirects to localhost or an internal address in production
Redirected to http://localhost:3000 after Google sign in on the live siteShort 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.
Fix it fast
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'))2Match what you see, then jump to its fix
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 output2Fix
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 windowConfirmed by supabase.com
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 Configuration2Fix
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 domainConfirmed by supabase.com
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
Web apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fixWeb apps
The shell cannot find create-next-app
command not found: create-next-app
create-next-app is not meant to be installed as a shell command.
2 causes, each with a fixWeb apps
ESLint fails with: Cannot find module 'next/babel'
ESLint stops on the first file with a Babel or parser error instead of listing lint results
next/babel is a Babel preset that ships inside the next package.
3 causes, each with a fixWeb apps
Firebase Auth says the API key is not valid
Firebase: Error (auth/api-key-not-valid.-please-pass-a-valid-api-key.)
The apiKey your app sends is either empty, wrong, or blocked.
4 causes, each with a fixWeb apps
Jest fails with SyntaxError: Unexpected token when testing a Next.js component
Jest failed to parse a file.
Jest only understands plain JavaScript until a transform and a few file mappings tell it more.
3 causes, each with a fix