Auth Docs

Authentication Overview

This guide covers the most common authentication strategies used in modern web applications.

1. Username & Password

Traditional form‑based login using a hashed password stored in the database.

async function login(email, password) {
  const res = await fetch('/api/login', {
    method: 'POST',
    headers: {'Content-Type':'application/json'},
    body: JSON.stringify({email, password})
  });
  const data = await res.json();
  if (res.ok) localStorage.setItem('token', data.token);
  else throw new Error(data.message);
}

2. JSON Web Tokens (JWT)

Stateless authentication using signed tokens.

{
  "alg": "HS256",
  "typ": "JWT"
}
.
{
  "sub": "1234567890",
  "name": "John Doe",
  "iat": 1516239022,
  "exp": 1516242622
}

3. OAuth 2.0

Delegated authentication with third‑party providers.

Grant TypeUse Case
Authorization CodeWeb server apps
ImplicitSingle‑page apps (legacy)
Client CredentialsMachine‑to‑machine
Resource Owner PasswordTrusted apps only

4. Multi‑Factor Authentication (MFA)

Add an extra verification step, such as OTP or push notification.

function verifyMFA(token, code) {
  return fetch('/api/mfa/verify', {
    method: 'POST',
    headers: {'Authorization':`Bearer ${token}`, 'Content-Type':'application/json'},
    body: JSON.stringify({code})
  });
}