Mendlark / Guides
Supabase auth redirect loop on Vercel: how to fix it
Your app logs in fine in the Lovable, Bolt or local preview. You deploy to Vercel, sign in, and get bounced straight back to the login page, or a confirmation email opens localhost:3000. This is almost always configuration, not your code. Work through the checks below in order.
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €491. Set the Site URL to your live domain
In the Supabase dashboard open Authentication → URL Configuration. The Site URL is the default place Supabase sends people after email confirmation, magic links and password resets. If it still says http://localhost:3000 or an old preview address, every email link goes there.
Set it to your real address, for example https://yourapp.com, with no trailing path.
2. Add every address you use to Redirect URLs
On the same page, Redirect URLs is the allow list. If the address your app asks to return to isn't on it, Supabase falls back to the Site URL, which can look like a loop. Add:
https://yourapp.com/**(your live domain, with the wildcard so any path is allowed)https://www.yourapp.com/**if you also serve the www version- Your Vercel preview pattern, if you test on preview deployments, e.g.
https://*-your-team.vercel.app/** http://localhost:3000/**so local development keeps working
3. Pass an explicit redirect in your code
Don't rely on the default. Tell Supabase where to come back to, built from the current origin so it works in every environment:
// email + password sign-up
await supabase.auth.signUp({
email, password,
options: { emailRedirectTo: `${window.location.origin}/auth/callback` }
})
// Google, GitHub, etc.
await supabase.auth.signInWithOAuth({
provider: 'google',
options: { redirectTo: `${window.location.origin}/auth/callback` }
})
Whatever you pass here must match an entry in Redirect URLs from step 2.
4. Check the OAuth provider's settings
For Google sign-in, the provider sends users back to Supabase first, not straight to your app. In Google Cloud Console, under your OAuth client, the Authorized redirect URI should be your Supabase callback, https://<your-project-ref>.supabase.co/auth/v1/callback, and your live domain should be listed under Authorized JavaScript origins. A mismatch here usually shows a redirect_uri_mismatch error rather than a silent loop, but it's worth checking while you're there.
5. Make sure the session actually survives the redirect
If the URLs are right and you still land back on /login, the app is probably not seeing the session it just got. Common causes:
- Next.js middleware can't read the session. With server-side rendering, the session lives in cookies. If you use
@supabase/ssr, the middleware has to refresh the session and pass the updated cookies on in its response. If it creates a fresh response and drops them, every request looks logged out and the middleware redirects to login again. - The callback route never exchanges the code. With the PKCE flow (the default for server-side setups) your
/auth/callbackroute must callsupabase.auth.exchangeCodeForSession(code)before redirecting the user on. - The login page itself is protected. If your "is logged in?" check runs on
/loginand/auth/callbacktoo, a half-finished login sends users in a circle. Exclude those routes. - A profile lookup fails. Some generated apps treat "no profile row" as "not logged in". If Row Level Security is on for the profiles table with no select policy for the signed-in user, the query returns nothing and the app bounces you to login. Add a policy that lets users read their own row.
6. Check your environment variables on Vercel
In Vercel, open the project, then Settings → Environment Variables, and confirm the Supabase URL and anon (public) key are set for Production, with the exact names your code uses (for example NEXT_PUBLIC_SUPABASE_URL for Next.js or VITE_SUPABASE_URL for Vite). Values are baked in when the site builds, so redeploy after changing them. Never put the service_role key in a browser-visible variable.
Quick checklist
- Site URL = live domain
- Redirect URLs include live domain, www, previews and localhost, each with
/** emailRedirectTo/redirectToset fromwindow.location.origin- Provider redirect URI =
https://<ref>.supabase.co/auth/v1/callback - Middleware passes refreshed cookies; callback exchanges the code; login routes unprotected
- Env vars set for Production, then redeployed
Stuck on this? Get it fixed for €49.
Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.
Get it fixed for €49Still stuck?
Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Start with a free scan.
Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.