Skip to content

Web Authentication Security Deep Dive

Implement secure authentication: session-based vs JWT, password hashing, and OAuth 2.0.

What you'll learn

  • Session-based vs token-based (JWT) authentication
  • Password hashing: bcrypt, argon2, scrypt
  • OAuth 2.0 authorization code flow with PKCE
  • Token storage: httpOnly cookies vs localStorage

Concept

Web Authentication Security Deep Dive

Authentication verifies who a user is. The two main approaches are session-based and token-based (JWT).

Session-Based Auth

Server creates session after login, stores in memory/Redis, sends session ID in httpOnly cookie. Pros: can revoke instantly. Cons: must store state, harder to scale.

JWT

A JWT is self-contained: header.payload.signature, base64-encoded and signed. Payload contains user ID, roles, expiry. Pros: stateless, scales well. Cons: cannot revoke before expiry, payload readable.

Password Hashing

Never store plaintext. Use bcrypt (adaptive), argon2 (memory-hard, OWASP recommended), or scrypt. These are slow by design. Never use MD5 or SHA-256 for passwords.

OAuth 2.0 + PKCE

For third-party login, use Authorization Code flow with PKCE. PKCE prevents code interception. Client generates code_verifier and code_challenge. Always use PKCE for SPAs.

Example

              const crypto = require('crypto');
const sessions = new Map();
function createSession(uid) { const sid = crypto.randomBytes(32).toString('hex'); sessions.set(sid, {uid, ts:Date.now()}); return sid; }
function createJWT(payload, secret) {
  const h = Buffer.from(JSON.stringify({alg:'HS256',typ:'JWT'})).toString('base64url');
  const b = Buffer.from(JSON.stringify({...payload, exp:Date.now()+3600000})).toString('base64url');
  const s = crypto.createHmac('sha256', secret).update(`${h}.${b}`).digest('base64url');
  return `${h}.${b}.${s}`;
}
function genPKCE() { const v = crypto.randomBytes(32).toString('base64url'); const c = crypto.createHash('sha256').update(v).digest('base64url'); return {verifier:v, challenge:c}; }
const secret = 'secret-key';
const token = createJWT({userId:123, role:'admin'}, secret);
console.log('JWT:', token.substring(0, 50) + '...');
const pkce = genPKCE();
console.log('PKCE verifier length:', pkce.verifier.length);
            

createSession stores server-side session. createJWT implements HS256 signing. genPKCE creates verifier/challenge for OAuth. Use bcrypt/argon2 for passwords.

Try it

  • Hash Generator

    Password hashing and JWT signing use crypto - see them in action.

Common mistakes

The mistake

Storing JWT in localStorage.

The fix

localStorage is accessible to JS, so XSS can steal token. Use httpOnly cookies.

The mistake

Using MD5 or SHA-256 for password hashing.

The fix

These are fast hashes. GPUs can brute-force billions/sec. Use bcrypt or argon2.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors