Demo, all content is generated
Question

Login on Vercel preview deployments always bounces me to the production site

Solved · 721 views · asked by lucy_hart · edited

I test every change on the Vercel preview URL before merging. Everything works there except login: after Google login I end up on the real production domain, logged in there, not on the preview. So I can't test logged-in features on previews at all.

What I’ve tried

Added NEXT_PUBLIC_SITE_URL to the Vercel env vars and use it as redirectTo. Also tried adding one preview URL to Supabase, which works until the next deploy gets a new URL.

Comment

2 answers

Marked as helpful by the asker
jonas_k · edited

Two things are sending you to production:

  1. NEXT_PUBLIC_SITE_URL is probably set for all environments in Vercel, so previews also say 'go back to prod'. Set it for Production only and on previews fall back to the current origin:
const origin = typeof window !== 'undefined'
  ? window.location.origin
  : process.env.NEXT_PUBLIC_SITE_URL
await supabase.auth.signInWithOAuth({
  provider: 'google',
  options: { redirectTo: `${origin}/auth/callback` },
})
  1. Supabase must allow the preview URLs. Redirect URLs support wildcards, so add:
https://*-your-team-slug.vercel.app/**

If a redirect isn't on the list, Supabase falls back to the Site URL, which is your production domain.

Comment
It was set for all 3 environments. Changed both things and preview login works. lucy_hart · edited
Keep the wildcard scoped to your team slug like that. Don't put https://*.vercel.app, then anyone's Vercel site is an allowed redirect. rafa_dev · edited
deploydan · edited

Alternative if the wildcard makes you uneasy: in Vercel, assign a fixed domain to your staging branch (staging.yourapp.com). Then there's one extra URL to allow and you test logged-in stuff there, not on every random preview.

Comment
I like that for the client demos, a fixed URL looks better too. lucy_hart · edited