OAuth 2.0, OpenID Connect, and PKCE for Frontend Auth Flows

Medium•
Comic-style OAuth 2.0, OpenID Connect, and PKCE flow showing a public browser client, code challenge, authorization code, code verifier exchange, ID token, access token, protected API, and Backend for Frontend option.

The frontend participates in OAuth/OIDC, but it should not become the trust anchor. The browser starts redirects, stores short-lived client state, receives callback parameters, and calls APIs or a Backend for Frontend. The authorization server authenticates the user and issues grants/tokens. Resource servers validate access tokens for the intended audience. Strong architecture makes these responsibilities explicit and keeps raw credentials out of places where cross-site scripting or third-party scripts can steal them.

Quick Decision Guide

Senior-level answer: For browser apps, use Authorization Code with PKCE. OAuth 2.0 grants access to resources; OpenID Connect adds identity through an ID token and discovery metadata; PKCE binds the authorization request to the token exchange so an intercepted authorization code is not enough.

Browser apps are public clients: they cannot keep client secrets. That means the important design choices are redirect validation, state, nonce, PKCE, token lifetime, refresh strategy, storage threat model, and whether a Backend for Frontend should keep provider tokens server-side.

Do not describe OAuth as simply "login." Strong interview answers separate authorization, identity, browser constraints, token handling, and operational failure modes.

Protocol Boundaries: OAuth, OpenID Connect, PKCE

TermFull formWhat it solvesWhat it does not solve
OAuth 2.0Open Authorization 2.0delegated access to resourcesuser identity by itself
OpenID Connectidentity layer on OAuth 2.0authentication claims and ID token validationAPI authorization policy by itself
PKCEProof Key for Code Exchangeprotects authorization code exchange for public clientsCSRF, issuer mix-up, token storage, or XSS by itself
OAuth answers: can this client get access to this resource?
OpenID Connect answers: which user authenticated at this issuer?
PKCE answers: is the party redeeming this code the one that started the flow?

A strong frontend answer keeps those questions separate. If you blur them, you end up using ID tokens as API access tokens, skipping nonce validation, or assuming PKCE makes every browser-storage choice safe.

Authorization Code with PKCE Flow

A browser app cannot safely store a client secret, so it uses Authorization Code with PKCE.

Browser app
  -> generate code_verifier
  -> derive code_challenge
  -> redirect to authorization server with state, nonce, code_challenge
Authorization server
  -> authenticate user
  -> redirect back with authorization code
Browser or Backend for Frontend
  -> exchange code + code_verifier
  -> receive tokens or create application session
ValuePurpose
code_verifiersecret generated by the client for this flow
code_challengederived value sent in the authorization request
statecorrelates response to request and helps defend redirect CSRF
noncebinds the ID token to this authentication request
exact redirect URIprevents codes being sent to attacker-controlled callbacks

Tokens: ID Token, Access Token, Refresh Token

TokenUsed byPurposeCommon frontend mistake
ID tokenclient applicationprove authentication facts about the userusing it to authorize API calls
access tokenresource server/APIauthorize access to protected resourcesstoring long-lived bearer tokens in browser storage
refresh tokenclient or secure server-side componentobtain new access tokensallowing uncontrolled silent refresh loops

Browser Threat Model and Storage Choices

PatternBenefitCost
in-memory tokenlower persistence after reloadrefresh and tab coordination become harder
session cookie via Backend for Frontendhides provider tokens from JavaScriptrequires CSRF-aware cookie/session design
localStorage/sessionStoragesimple persistencelarger XSS blast radius for bearer tokens

Backend for Frontend vs Pure SPA

A pure Single Page Application can run Authorization Code with PKCE, but it still has to handle token exposure, refresh, logout, and cross-tab coordination in JavaScript.

A Backend for Frontend (BFF) changes the boundary:

Browser -> BFF session cookie -> BFF holds provider tokens -> APIs

BFF strengths

•provider tokens are not exposed to browser JavaScript
•refresh is centralized
•API calls can be shaped for the frontend
•logout and session revocation are easier to coordinate

BFF costs

•more backend infrastructure
•CSRF and cookie policy must be designed correctly
•session scaling, deployment, and observability become part of the auth system

The staff-level answer is not "always use BFF." It is: use BFF when token exposure, enterprise controls, auditability, or refresh reliability justify the operational cost.

Failure Modes and Recovery Policy

Failure modeSymptomBetter policy
callback URI mismatchuser stuck in redirect errorexact environment-specific redirect configuration
missing state validationlogin CSRF or response confusiongenerate, store, and verify state per request
missing nonce validationID token replay/confusion riskvalidate nonce for OpenID Connect flows
refresh race across tabsrepeated 401s or refresh stormsingle-flight refresh and broadcast auth state
provider outagelogin and refresh failbounded retries, clear re-auth UX, incident messaging
token accepted by wrong APIconfused resource accessvalidate audience and issuer at resource servers

Senior Interview Answer Pattern

PromptStrong response
Does PKCE replace state?No. PKCE protects code exchange; state correlates the redirect response and helps defend redirect CSRF.
Is OAuth authentication?OAuth is authorization. OpenID Connect adds authentication semantics.
Can a SPA keep a client secret?No. Browser apps are public clients.
Is localStorage always forbidden?The better answer is threat-model based, but bearer tokens in JavaScript-accessible storage increase XSS blast radius.

Key Takeaways

1OAuth 2.0 grants delegated access; OpenID Connect adds identity; PKCE protects public-client code exchange.
2Browser apps are public clients and cannot safely keep client secrets.
3PKCE does not replace state, nonce, redirect URI validation, issuer validation, or token audience checks.
4ID tokens are for the client; access tokens are for APIs; refresh tokens extend access and need stricter handling.
5A Backend for Frontend can reduce token exposure but adds cookie, CSRF, session, and operational responsibilities.
6Staff-level answers treat auth refresh, logout, provider outages, and redirect loops as reliability design problems.