Switch language한국어
Back to the list

How to build a login flow in Next.js 15 (sessions, cookies, CSRF, and the timing attack nobody talks about)

TL;DR AI

Key summary

2 min read
  1. The article explains a hardened Next.js 15 login flow using session cookies, CSRF protection, hashed session storage, and timing-attack defenses.

  2. A login form posts credentials to an auth endpoint, which verifies passwords and creates a session row in Postgres before setting a cookie for later requests.

  3. Only a SHA-256 hash of the session token is stored, along with session metadata, to reduce replay risk if the database is leaked.

  4. The design also adds CSRF checks and timing-attack mitigation, while avoiding brittle practices like binding sessions to IP addresses.

Read the original