Authentication done right

Hash passwords properly, resist guessing attacks and build login and reset flows that do not leak information.

Intermediate40 min

Learning objectives

  • Store passwords with a slow, salted, memory-hard or key-derivation hash
  • Compare secrets in constant time
  • Design login, lockout and password reset flows that resist abuse

Never store plaintext passwords

If your database leaks, plaintext passwords expose every account, and users reuse passwords across sites. Store a password hash that is:

  • Slow and salted, so attackers cannot test billions of guesses cheaply.
  • Produced by a purpose-built password function such as Argon2id, scrypt or bcrypt.

Fast general-purpose hashes such as SHA-256 or MD5 are not suitable for passwords.

Here is a dependency-free example using Node’s built-in scrypt:

import { scrypt as scryptCb, randomBytes, timingSafeEqual } from 'node:crypto';
import { promisify } from 'node:util';

const scrypt = promisify(scryptCb);

export async function hashPassword(password) {
  const salt = randomBytes(16);
  const hash = await scrypt(password, salt, 64);
  return `scrypt$${salt.toString('base64')}$${hash.toString('base64')}`;
}

export async function verifyPassword(password, stored) {
  const [, saltB64, hashB64] = stored.split('$');
  const expected = Buffer.from(hashB64, 'base64');
  const actual = await scrypt(password, Buffer.from(saltB64, 'base64'), expected.length);
  return timingSafeEqual(actual, expected);
}

The comparison uses timingSafeEqual so response timing does not reveal how many bytes matched. In production, Argon2id from a well-maintained library is a strong choice, with parameters tuned to your server.

Resist guessing

  • Rate limit login attempts per account and per IP address. Back off progressively.
  • Use generic error messages. “Invalid email or password” is better than “No account with that email”, which enables account enumeration.
  • Enforce length, not complexity. Allow long passphrases and check candidates against known breached-password lists.
  • Add multi-factor authentication. Passkeys (WebAuthn) and TOTP codes stop most credential-stuffing attacks.

Password reset

Password reset is a common weak point. A solid design:

  1. Respond with the same message whether or not the email exists.
  2. Generate a random token of at least 128 bits and store only its hash.
  3. Set a short expiry, such as 15 to 60 minutes.
  4. Make the token single-use and invalidate it after a successful reset.
  5. Invalidate existing sessions after the password changes.
  6. Notify the user by email that the password was changed.

Try it: PIN generator and verifier

The PIN tool applies these ideas to short numeric PINs. It generates a PIN with a secure random source, enrols it as a salted PBKDF2 hash, and locks verification after five failed attempts. Try a wrong PIN several times to see the lockout.

Lab: review a login route

Pick a login handler you have written, or a sample one, and check:

  1. Is the password hashed with a slow, salted function?
  2. Does a failed login take roughly the same time for unknown and known emails?
  3. Is there rate limiting?
  4. Do error messages reveal whether an account exists?

Key takeaways

  • Hash passwords with Argon2id, scrypt or bcrypt, never with fast hashes.
  • Make guessing expensive with rate limits, and add MFA where you can.
  • Keep reset tokens random, hashed, short-lived and single-use.