Backend & Node.js · APIs, data and failure
How does the OAuth 2.0 authorization code flow with PKCE work?
Short answer
The client redirects the user to the authorization server with a code challenge, the user authenticates and consents, and the client exchanges the returned code plus verifier for tokens. PKCE prevents code interception; state prevents CSRF.
OAuth2 is an authorization framework, not a login protocol. The client asks for scoped access to a resource on behalf of a user. The authorization code flow keeps tokens out of the browser URL by exchanging a short-lived code on a back channel.
The flow
- The client generates a random code verifier and a challenge derived from it
- It redirects the user to the authorization endpoint with client id, redirect URI, scope, state and challenge
- The user authenticates and consents at the authorization server
- The server redirects back with a code and the original state
- The client verifies state, then exchanges the code and verifier at the token endpoint
- The server returns an access token, optional refresh token and, with OpenID Connect, an id token
GET /authorize?response_type=code
&client_id=web-app
&redirect_uri=https%3A%2F%2Fapp.example.com%2Fcallback
&scope=openid%20profile%20orders.read
&state=random-value
&code_challenge=base64url-sha256
&code_challenge_method=S256 HTTP/1.1
Host: auth.example.comThe client secret must never live in a public browser or mobile app. PKCE exists so public clients can use the code flow safely. Confidential server-side clients still use a secret in addition. The implicit flow is deprecated for new applications because it exposes tokens in the URL.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What problem does PKCE solve?
- 2What is the state parameter for?
- 3Why is the implicit flow discouraged?
- 4How does OpenID Connect differ from OAuth2?
Say it in the interview
In the authorization code flow the client sends the user to the authorization server with a PKCE challenge and a state value. After consent, the server redirects back with a short-lived code. The client verifies state and exchanges the code plus verifier for tokens on the back channel. PKCE protects public clients from code interception, and OpenID Connect adds the identity layer with an id token.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Backend & Node.js mock interview and get scored feedback on depth, clarity and confidence.