OAuth 2.0 and OpenID Connect

Delegate access and add 'Sign in with Google/GitHub' correctly using standard flows.

Intermediate · about 7 hours · difficulty 4/5

Learn first: Token-Based Auth and JWT

OAuth 2.0 is a framework for delegated authorization: letting an app access a user's resources on another service (read their GitHub repos, their Google Calendar) without ever seeing their password. OpenID Connect (OIDC) is a thin identity layer on top of OAuth 2.0 that adds an ID token (a JWT describing who the user is), which is what powers "Sign in with Google".

Roles

The resource owner (the user), the client (your app), the authorization server (Google, Auth0, Keycloak) and the resource server (the API holding the data).

The flow to know: Authorization Code with PKCE

  1. Your app redirects the user to the authorization server with client_id, requested scope, redirect_uri, a random state and a PKCE code challenge.
  2. The user logs in and consents.
  3. The server redirects back with a short-lived code.
  4. Your backend exchanges the code (plus the PKCE verifier) for tokens.

Other grants: client credentials for machine-to-machine calls. The implicit and password grants are deprecated.

Build or buy

Identity is easy to get subtly wrong. Many teams use a provider (Auth0, Clerk, Cognito, Firebase Auth) or self-hosted Keycloak instead of writing their own authorization server. You should still understand the flow to integrate and debug it.

Pitfalls

  • Skipping state validation, enabling CSRF on the login flow.
  • Accepting ID tokens without checking iss, aud and signature.

You are ready to move on when you have implemented "Sign in with GitHub or Google" end to end and can explain each redirect and parameter.

Key concepts

  • OAuth roles and scopes
  • Authorization code flow with PKCE
  • Client credentials grant
  • OpenID Connect and ID tokens
  • Social login providers
  • Keycloak, Auth0, Cognito
  • Token introspection

Mini project

Add Sign in with GitHub to your app using the authorization code flow with PKCE, linking the external identity to a local user record.

Check yourself

What is the difference between an OAuth access token and an OIDC ID token, and which one should your API accept?

Free resources