Software Studio
Back to Articles

OAuth 2.0 and OpenID Connect: A Practical Guide

SecurityOAuthAuthentication
Cover illustration for OAuth 2.0 and OpenID Connect: A Practical Guide

OAuth 2.0 and OpenID Connect are foundational to modern authentication, and they're also some of the most misunderstood technologies in web development. After implementing auth systems across dozens of applications, here's the practical knowledge that matters.

OAuth 2.0 vs OpenID Connect

  • OAuth 2.0 is about authorization: granting limited access to resources.
  • OpenID Connect (OIDC) is a layer on top that adds authentication: verifying who a user is.

When people say "OAuth login," they usually mean OIDC. The distinction matters because it determines which tokens you use and how you validate them. An access token tells an API what a caller may do. It was never designed to tell your app who signed in.

Use the Authorization Code flow with PKCE

The Authorization Code flow with PKCE is the correct choice for virtually every modern application: web apps, mobile apps and SPAs.

The implicit flow is deprecated. The password grant is deprecated. If someone tells you to use either of these, push back.

The flow itself has five steps:

  1. Your app generates a random secret, the code verifier, and sends a hash of it along with the login request.
  2. The user signs in with the identity provider.
  3. The provider redirects back to your app with a short-lived authorization code.
  4. Your app exchanges the code for tokens, sending the original verifier.
  5. The provider checks the verifier against the hash and issues the tokens.

PKCE (Proof Key for Code Exchange) prevents authorization code interception attacks. Someone who steals the code in step 3 can't use it, because they don't have the verifier. It also works without a client secret, making it safe for public clients such as mobile apps and SPAs.

The three tokens

Tokens come in three flavors.

  • The access token grants API access. It's short-lived, from minutes to hours, and should never be stored in localStorage.
  • The refresh token gets new access tokens. It's long-lived and must be stored securely, in an HttpOnly cookie or the OS keychain.
  • The ID token proves who the user is. It's a JWT containing user claims. Validate it on receipt, extract the claims, then you can discard it.

The reason for avoiding localStorage is that any script running on your page can read it. One cross-site scripting bug and the token is gone. An HttpOnly cookie can't be read by JavaScript at all.

Validate JWTs properly

JWT validation is critical and frequently done wrong. There are four checks:

  1. Verify the signature using the provider's public keys, fetched from the JWKS endpoint.
  2. Check the expiration (the exp claim).
  3. Verify the audience (the aud claim) matches your client ID.
  4. Verify the issuer (the iss claim) matches your provider.

Skip any of these checks and you have a security vulnerability. A token with a valid signature but the wrong audience was issued for a different application, and accepting it means accepting logins that were never meant for you.

Use a well-maintained library for this:

import { createRemoteJWKSet, jwtVerify } from "jose";

const jwks = createRemoteJWKSet(
  new URL("https://example.auth0.com/.well-known/jwks.json"),
);

const { payload } = await jwtVerify(token, jwks, {
  issuer: "https://example.auth0.com/",
  audience: process.env.AUTH0_CLIENT_ID,
});

Decoding a JWT is not the same as verifying it. Anyone can read or forge an unsigned payload.

In Next.js, let the SDK do the work

For server-rendered applications like Next.js, the Auth0 SDK handles the heavy lifting. It:

  • Manages the authorization code flow.
  • Stores tokens in encrypted HttpOnly cookies.
  • Provides session management.
  • Handles token refresh transparently.

Don't build this yourself unless you have a very specific reason. Authentication is the last place to be creative.

What the SDK won't do is decide who may do what. Check the session on the server for every protected page, API route and server action. Hiding a button in the UI is not access control.

Role-based access control

RBAC (Role-Based Access Control) builds on top of OIDC. Assign roles in your identity provider, include them as custom claims in the ID token, and check them in your application's authorization layer.

Auth0's RBAC system lets you define permissions at the API level and assign them to roles, which are assigned to users. Your code checks for a permission such as articles:publish, and who holds that permission can change without a deploy. On the API side, guards in NestJS are a clean place to enforce it.

The security checklist

  • Always use HTTPS.
  • Always use HttpOnly cookies for token storage.
  • Always validate tokens on the server side.
  • Never expose client secrets in frontend code.
  • Always use PKCE.
  • Rotate signing keys periodically.

These aren't optional.

Multi-tenant applications

Multi-tenant applications add complexity. Each tenant might use a different identity provider: one uses Google Workspace, another uses Azure AD, a third uses username and password.

Protocols like SAML and OIDC provide a standard interface across these providers. Auth0's Enterprise Connections handle this federation transparently, so your application sees the same kind of token whichever provider the user signed in with.

The short version

Use OIDC for login. Use the Authorization Code flow with PKCE. Keep tokens in HttpOnly cookies. Validate signature, expiry, audience and issuer every time. Let a proven SDK run the flow, and spend your own effort on authorization, which is the part no library can decide for you.