# 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. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/full-stack/oauth-and-social-login
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/full-stack/sessions-vs-jwt)

"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

1. Your app redirects the user to the provider with a client id, redirect URI, scopes, a **state** value and a **PKCE** challenge.
2. The user logs in and consents.
3. The provider redirects back with a short-lived **code**.
4. Your server exchanges the code (plus client secret and PKCE verifier) for tokens.
5. 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](https://oauth.net/2/) (oauth.net)
- [OpenID Connect](https://openid.net/developers/how-connect-works/) (OpenID Foundation)
- [Authorizing OAuth apps](https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/authorizing-oauth-apps) (GitHub)

Previous: [Sessions, Cookies and JWTs](https://www.internhack.in/roadmaps/tracks/full-stack/sessions-vs-jwt) · Next: [Auth with Auth.js (NextAuth)](https://www.internhack.in/roadmaps/tracks/full-stack/authjs)

## More in Authentication, Authorization and Security

- [Authentication Fundamentals and Password Storage](https://www.internhack.in/roadmaps/tracks/full-stack/auth-fundamentals)
- [Sessions, Cookies and JWTs](https://www.internhack.in/roadmaps/tracks/full-stack/sessions-vs-jwt)
- [OAuth 2.0, OpenID Connect and Social Login](https://www.internhack.in/roadmaps/tracks/full-stack/oauth-and-social-login)
- [Auth with Auth.js (NextAuth)](https://www.internhack.in/roadmaps/tracks/full-stack/authjs)
- [Auth with a Hosted Provider (Clerk)](https://www.internhack.in/roadmaps/tracks/full-stack/clerk)
- [Authorization and Access Control](https://www.internhack.in/roadmaps/tracks/full-stack/authorization-rbac)
- [Web Security Essentials](https://www.internhack.in/roadmaps/tracks/full-stack/web-security)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Full-Stack Developer Roadmap](https://www.internhack.in/roadmaps/tracks/full-stack)
