Track 07 · Lesson 13 of 18Identity and login
- 01Authentication vs authorization
- 02Browser storage compared
- 03Sessions versus tokens
- 04Inside a JWT
- 05Access and refresh tokens
- 06Where to keep tokens
- 07Choosing a design
- 08The login lifecycle
- 09The token clock
- 10Dead or just unlucky? Refresh decisions
- 11Inside a secure session cookie
- 12Five tabs, one refresh
- 13“Log in with Google”: OAuth and PKCE
- 14Two-factor login
- 15Storing passwords
- 16Logging out properly
- 17Trade-offs and common mistakes
- 18Compare the designs
“Log in with Google”: OAuth and PKCE
Lesson 13 of 18· 8 min
OAuth 2.0 lets an app get limited access to your account on another service without ever seeing your password. OpenID Connect (OIDC) adds an ID token that says who you are. Modern apps use the authorization code flow with PKCE.
Log in with Google, step by step
Then let a malicious extension steal the code
1. You click “Log in with Google”
The app prepares the request. For PKCE it creates a random secret, the code verifier, and keeps it to itself. It sends Google only a hash of it, the code challenge. It also creates a random state value to recognize the reply later.
code_verifier (stays in the app): code_challenge = SHA-256(verifier): state:
Access versus identity#
OAuth is about access: an access token that lets the app call another service’s API, limited by the scope you approved. OIDC is about identity: an ID token, which is a JWT saying who you are. “Log in with Google” uses both. The app verifies the ID token (signature, issuer, audience, expiry) and then creates its own session, exactly like a normal login.
state and PKCE#
Two random values protect the flow. state is generated when the flow starts, stored, and compared when the provider sends you back: a mismatch means the callback wasn’t started by this user, so refuse it. PKCE (Proof Key for Code Exchange) creates a secret verifier, sends only its SHA-256 hash at the start, and must present the original when exchanging the code. A stolen code is useless without the verifier, which never left the app. Delete both values once they’re used.
What’s no longer recommended#
The old “implicit flow” put tokens straight into the URL, where history, logs and extensions could read them. Public clients, like single-page and mobile apps, should always use the authorization code flow with PKCE.
Check yourself