APIs & Integrations

OAuth & Authentication

Authentication and Authorization are the foundation of secure applications. Master OAuth 2.0 with PKCE, JWT tokens, token storage, refresh strategies, and the security patterns that protect your users.

How does OAuth login work?

OAuth 2.0 lets users sign in with an existing account (Google, GitHub) without giving your app their password. Your app redirects the user to the provider, the user approves, and the provider redirects back with a code. Your server exchanges the code for tokens. For browser apps, use the Authorization Code flow with PKCE, and keep tokens in HTTP-only cookies.

OAuth 2.0 Grant Types

Auth Code + PKCE✅ Recommended

Web/SPA/mobile apps. Safest for public clients. Prevents code interception.

Client Credentials✅ Server-to-server

Machine-to-machine. No user involved. Use for microservice auth.

Device Code⚠️ Special devices

Smart TVs, CLIs. User authorizes on a separate device.

Implicit❌ Deprecated

Tokens in URL fragment — avoid completely. Replaced by PKCE.

Password❌ Deprecated

Passes raw username/password. Only for legacy systems you own.

JWT Standard Claims

sub
Subject: User/entity identifier (e.g. user ID)
iss
Issuer: Token issuer URL (e.g. https://auth.example.com)
aud
Audience: Intended recipient(s) of the token
exp
Expiration Time: Unix timestamp — token invalid after this
iat
Issued At: When the token was issued
nbf
Not Before: Token not valid before this time
jti
JWT ID: Unique token identifier — enables revocation
name
Full name: OIDC claim — user's display name
email
Email: OIDC claim — user's email address
roles
Roles (custom): Application-defined user roles/permissions

Common Pitfalls

Storing tokens in localStorage — accessible by any JS on the page (XSS attack vector)
Store access tokens in memory (JS variable) and refresh tokens in httpOnly cookies set by the server.
Trusting JWT validation only on the client — trivially bypassable
Always validate JWT signature and expiry on the server for every protected request. Client check is UX only.
Not using PKCE for SPAs — authorization code interception attack is possible
Always use PKCE (code_challenge/code_verifier) for any public client. It is now mandatory per OAuth 2.1.
Forgetting to verify the 'state' parameter on callback — CSRF vulnerability
Generate a random state, store it before redirect, verify it on callback. Mismatch = reject the response.

Interactive Demo

Code Editor

<p>OAuth "Sign in with GitHub" flow (Authorization Code + PKCE):</p>
<ol>
  <li>Your app redirects to the provider with <code>client_id</code>, <code>state</code> and a <code>code_challenge</code>.</li>
  <li>The user logs in and approves.</li>
  <li>The provider redirects back with <code>?code=…&amp;state=…</code>.</li>
  <li>Your <b>server</b> exchanges the code for tokens (the secret never touches the browser).</li>
  <li>The server sets an HTTP-only session cookie.</li>
</ol>
<button id="build">Build step 1 URL</button>
<pre id="out"></pre>

Preview

OAuth and Authentication: summary

Key points

  • Never put client secrets in front-end code; the code-for-token exchange happens on the server.
  • OpenID Connect adds identity (who the user is) on top of OAuth's authorisation.
  • Libraries like Auth.js (NextAuth), Clerk or Firebase Auth handle the details safely.
  • Validate the state parameter to prevent CSRF during the redirect.

Frequently asked questions

›Where should I store auth tokens in the browser?

In secure, HTTP-only, SameSite cookies set by your server. Avoid localStorage, which any injected script can read.