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
Web/SPA/mobile apps. Safest for public clients. Prevents code interception.
Machine-to-machine. No user involved. Use for microservice auth.
Smart TVs, CLIs. User authorizes on a separate device.
Tokens in URL fragment — avoid completely. Replaced by PKCE.
Passes raw username/password. Only for legacy systems you own.
JWT Standard Claims
Common Pitfalls
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=…&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
stateparameter 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.