Short answer
PKCE (Proof Key for Code Exchange, pronounced ‘pixie’) is an extension to the OAuth code flow. The app creates a random secret, sends only its hash when the login starts, and must show the secret to swap the returned code for tokens. A code that leaks on the way back is useless without it. Supabase supports PKCE; the OAuth security best practice requires it for browser apps.
Ask an AI about this page:ChatGPTClaude
On this page
PKCE (Proof Key for Code Exchange, RFC 7636) is an extension to the OAuth authorization code flow that ties the returned code to the client that started the login. Before redirecting the user, the app creates a random string, the code verifier, and sends only a hash of it, the code challenge. To swap the code for tokens afterwards, it must present the original verifier. Someone who intercepts the code alone cannot use it.
The RFC defines two methods: plain, where challenge and verifier are the same, and S256, a SHA-256 hash. Clients that can use S256 must use it. The OAuth security best practice, RFC 9700, says browser apps and other public clients must use PKCE and should avoid the older implicit flow.
PKCE in an AI-built app
In a Supabase app you rarely see PKCE directly. With flowType: "pkce" in the client options, or with Supabase’s server-side helpers, the library creates and stores the verifier when the flow starts. After the user signs in with Google or clicks a link, they land on your app with a code query parameter, which exchangeCodeForSession(code) (or the client’s automatic URL detection) swaps for a session.
The alternative is the implicit flow, where the access and refresh tokens arrive in the URL fragment (#access_token=…). Tokens in a URL can end up in browser history and in scripts on the page. With PKCE only a short-lived, single-use code travels through the URL: Supabase documents that it is valid for five minutes and can be exchanged once.
The trade-off is that the verifier lives in the browser that started the flow. A magic link opened on a phone after requesting it on a laptop cannot complete. That is expected behaviour, not a bug to “fix” by switching back to implicit.
How to check your app
- Find where the Supabase client is created (often
src/integrations/supabase/client.ts). Look forflowType. No value means the library default applies; check the default of the version you use. - Open your callback route. A PKCE app reads a
codequery parameter and callsexchangeCodeForSession. An app that parses#access_tokenfromwindow.location.hashis on the implicit flow. - If you use a server-side framework, check that the code verifier is stored in cookies, not in
localStorage, which the server cannot read.
How to fix or prevent it
Set the flow to PKCE, exchange the code on your callback page and only redirect to same-origin paths afterwards. Email links need a template that sends a token hash for PKCE; Supabase’s docs describe the change. The sign-in pitfalls around confirmation and reset links are collected in Supabase Auth pitfalls, and a callback that forwards to any URL is covered in Open redirect after login.
Frequently asked questions
Why does my magic link or login fail when opened on another device?
With PKCE the code verifier is stored in the browser that started the flow. Supabase’s docs say the exchange must happen on the same browser and device. Open the link where you requested it.
How long is a Supabase PKCE code valid?
Supabase documents five minutes, and the code can be exchanged only once. After that the user has to start the flow again.
Do I still need the state parameter with PKCE?
RFC 9700 says clients must prevent CSRF and that PKCE can do that job, provided the authorization server supports PKCE. Supabase handles this inside its client libraries.
Sources
- 1.RFC 7636: Proof Key for Code Exchange by OAuth Public Clients — IETF
- 2.RFC 9700: Best Current Practice for OAuth 2.0 Security — IETF
- 3.PKCE flow — Supabase Docs
- 4.Passwordless email logins — Supabase Docs