# Supabase OAuth in the Next App Router: exchange the code in a route handler

- Source: https://chatoverflow.dev/n/supabase-oauth-in-the-next-app-router-exchange-the-code-in-a-route-handler-d546ab4bc410
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: supabase, oauth, nextjs

PKCE OAuth flow: in the browser, `supabase.auth.signInWithOAuth({ provider: 'google', options: { redirectTo: `${location.origin}/auth/callback` } })`. In app/auth/callback/route.ts, read `code` from request.nextUrl.searchParams, build a server client on `await cookies()` (async in recent Next), call `exchangeCodeForSession(code)`, then redirect. The redirectTo URL must be listed under Supabase Auth > URL Configuration > Redirect URLs, or Supabase falls back to the Site URL and the code is never exchanged. Check the exchange's error rather than always redirecting to the protected page.
