OAuth 2.0, OpenID Connect and Social Login
Let users sign in with Google or GitHub using the authorization code flow, and understand what OAuth does and does not prove.
Intermediate · about 5 hours · difficulty 4/5
Learn first: Sessions, Cookies and JWTs
"Sign in with Google" is expected in most student-facing apps. It is built on OAuth 2.0 (delegated access) and OpenID Connect (identity on top of OAuth).
The authorization code flow
- Your app redirects the user to the provider with a client id, redirect URI, scopes, a state value and a PKCE challenge.
- The user logs in and consents.
- The provider redirects back with a short-lived code.
- Your server exchanges the code (plus client secret and PKCE verifier) for tokens.
- You read the verified identity (email, name) from the ID token or userinfo endpoint and create or link a local user.
Important details
- state protects against CSRF on the callback; PKCE protects the code exchange.
- Redirect URIs must match exactly; register both localhost and production URLs.
- Only trust emails the provider marks as verified before linking accounts.
- Keep the client secret on the server.
When to use a library
You should understand this flow, but in production use a well-tested library (Auth.js, Clerk or your provider's SDK) rather than writing it by hand.
Pitfalls
- Using the implicit flow, which is deprecated.
- Linking a social login to an existing account just because the email matches an unverified address.
- Requesting far more scopes than you need.
You are ready to move on when you can draw the authorization code flow with PKCE and explain what each step protects against.
Key concepts
- OAuth 2.0 vs OpenID Connect
- Authorization code flow
- PKCE and state
- Scopes and consent
- Redirect URIs
- Account linking
- Google and GitHub providers
Mini project
Register a GitHub OAuth app and implement the authorization code flow manually in a small Express or Next.js app (for learning), printing each step, then compare it with the library version.
Check yourself
What attack does the state parameter prevent, and what does PKCE protect?
Free resources
- OAuth 2.0 (oauth.net)
- OpenID Connect (OpenID Foundation)
- Authorizing OAuth apps (GitHub)