Key points
- Two round trips: authorize redirect → callback with code → POST to /token → access token (+ ID token, refresh token).
- Always pair with PKCE (mandatory for public clients, recommended for all).
- Replaces the deprecated Implicit Flow for SPAs and mobile.
- Use the state parameter (CSRF) and nonce (OIDC replay protection).
- OAuth 2.1 and OIDC define this as the default and recommended grant type.
What is the Authorization Code Flow?
The Authorization Code Flow is the OAuth 2.0 grant type that virtually every web, mobile, and SPA app should use. It's a two-step dance designed so that powerful tokens never live in the browser URL bar or browser history.
How it works (with PKCE)
- Client generates a random
code_verifierand computescode_challenge = SHA256(code_verifier). - Browser redirect to
/authorizewith:response_type=code,client_id,redirect_uri,scope,state(CSRF),nonce(OIDC),code_challenge,code_challenge_method=S256. - User authenticates at the authorization server (passkey, password+MFA, SSO).
- Authorization server redirects back to
redirect_uriwith?code=<one-time>&state=<echo>. - Client verifies
statematches, then POSTs to/token:grant_type=authorization_code,code,redirect_uri,code_verifier, and (for confidential clients)client_id+client_secret. - Token endpoint returns
access_token, optionallyid_token(OIDC), and optionallyrefresh_token.
Why this dance
- The code is one-time, short-lived (≤60s typically), and useless without the verifier.
- The tokens are delivered over a back-channel POST, not in URL fragments — they never appear in browser history, referrer headers, or server logs.
- The
stateparameter prevents CSRF on the callback. - The
nonceparameter (OIDC) prevents ID-token replay.
Common misconceptions
- "Implicit Flow is faster." It's deprecated. Use Auth Code + PKCE.
- "Client secret protects everything." Not for public clients (SPAs, mobile) — they can't keep one. That's exactly why PKCE exists.
- "
stateis optional." Treat it as required. Skipping it opens you to CSRF on the callback.
Editorial note
If you're integrating OAuth from scratch, use a vetted library (oauth4webapi, MSAL, openid-client, AppAuth). The flow is simple in principle and full of footguns in practice.
