Implement OAuth 2.0 authorization code flow with PKCE for SPAs and native apps

domain: identity-general · 6 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Generate a cryptographically random code_verifier string (recommended 43-128 characters) and derive a code_challenge by applying SHA-256 and then Base64URL-encoding the result.
  2. Redirect the user to the authorization endpoint with response_type=code, client_id, redirect_uri, scope, state, code_challenge, and code_challenge_method=S256.
  3. After the user authenticates, the authorization server redirects back with an authorization code; verify the state parameter matches the one you sent to prevent CSRF.
  4. Exchange the authorization code at the token endpoint by POSTing code, redirect_uri, client_id, grant_type=authorization_code, and the original code_verifier (not the challenge).
  5. Receive and securely store the access token and refresh token; for SPAs prefer in-memory storage; for native apps use the platform secure store.
  6. Refresh the access token using the refresh token when it expires, and implement token rotation to detect refresh token reuse.

Known gotchas

Related routes

Implement OAuth 2.0 authorization code flow with PKCE for a public client
oauth.net · 6 steps · unrated
Implement Spotify OAuth Authorization Code flow with PKCE
developer.spotify.com · 5 steps · unrated
Migrate an OAuth client from the deprecated implicit grant to the OAuth 2.1 authorization code flow with mandatory PKCE
oauth.net/2.1 · 5 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans