Magic link emails redirecting to localhost:8080 instead of custom domain
nikhilmohanv
HOBBYOP

2 months ago

I recently migrated my Next.js app from one Railway project to another and now Supabase magic link emails are redirecting to localhost:8080 instead of my custom domain.

My setup:

  • Next.js + Supabase auth (magic link + Google OAuth)
  • Custom domain linked to the Railway project
  • NEXT_PUBLIC_APP_URL set to my production domain in Railway variables
  • emailRedirectTo in code uses process.env.NEXT_PUBLIC_APP_URL (not window.location.origin)
  • Everything works fine on Vercel staging with the same codebase and env vars

The issue only happens on Railway — the generated email link has localhost:8080 baked into it. Port 8080 is what Railway uses internally.

$10 Bounty

4 Replies

nikhilmohanv
HOBBYOP

2 months ago

When I point the custom domain back to the old Railway project and test using the new project's .up.railway.app URL directly — the redirect works correctly (goes to startupgrantsindia.com as expected). But the moment I attach the custom domain to the new project, it starts redirecting to `localhost:8080


2 months ago

Are you using a Dockerfile or Railpack? Next.js needs NEXT_PUBLIC_* to be available at build time for them to work

If you're indeed using a Dockerfile, add a ARG NEXT_PUBLIC_APP_URL somewhere


2 months ago

Also means you may have to redeploy after adding the (custom) domain


bzuhair
HOBBY

2 months ago

can you show us the magic-link href structure?

If the email link is a Supabase URL like https://<project>.supabase.co/auth/v1/verify?...&redirect_to=http://localhost:8080 then your app passed the wrong emailRedirectTo, or Supabase fell back to the wrong Site URL.

If the email link itself starts with something like http://localhost:8080/auth/confirm?... then your email template is using {{ .SiteURL }} and your Supabase Site URL is wrong.


Welcome!

Sign in to your Railway account to join the conversation.

Loading...