Password Hashing and JWT Authentication

Passwords should never be stored as plaintext. Password hashing algorithms such as Argon2 or bcrypt are designed to make password guessing computationally expensive.

JSON Web Tokens can carry signed claims between systems. A JWT can be useful for stateless API authentication, but token storage, expiration, rotation, revocation, audience validation, and signing-key management must be designed carefully.

Never Trust JWT Claims Blindly

Always verify the signature and validate relevant claims such as issuer, audience, expiration, and intended use.

JWT Verification Concept
import jwt from 'jsonwebtoken';

function authenticate(req, res, next) {
  const token = req.headers.authorization?.replace('Bearer ', '');

  if (!token) {
    return res.status(401).json({ error: 'Missing token' });
  }

  try {
    req.user = jwt.verify(token, process.env.JWT_SECRET);
    next();
  } catch {
    res.status(401).json({ error: 'Invalid token' });
  }
}
💡 The middleware extracts a Bearer token and verifies its signature before allowing the request to continue.
Key Takeaways
  • Never store plaintext passwords.
  • JWT verification must validate both the signature and security-relevant claims.
  • Short-lived access tokens and controlled refresh-token strategies reduce risk.
Knowledge Check

How should user passwords be stored?