Home / Learn Secure SQL / Project: Secure Password Reset Flow

Project: Secure Password Reset Flow

Build a reset-token flow that cannot be guessed or reused.

Quick summaryBuild a reset-token flow that cannot be guessed or reused.

How to think about this

Reset token lifecycletoken generated(secure random)emailed to userverified: not used,not expiredmarked usedimmediately after…
Reset token lifecycle

The basic idea

A password reset flow issues a single-use, random, time-limited token stored in the database, emails a link containing it, and verifies it (bound parameter lookup, expiry check, and immediate invalidation after use) before allowing a password change.

Why this comes up

Real projects are where secure patterns become habits instead of theory.

The mechanics

Work through the steps in order; do not skip the checklist.

Where it is used

  • Portfolio project
  • Code review practice
  • Interview preparation

What you will build

A password reset flow issues a single-use, random, time-limited token stored in the database, emails a link containing it, and verifies it (bound parameter lookup, expiry check, and immediate invalidation after use) before allowing a password change.

LevelTime needed
Intermediate3-4 hours

What you need (tools and setup)

ToolWhat it isWhy you need itCost
Code editor (VS Code or similar)The program you write code in.You need somewhere to write and run the example app.free
A language runtime (Node.js, Python, PHP or similar)Installs the interpreter so your code can run on your computer.The project examples run as small scripts.free
SQLite (bundled, no install needed)A tiny file-based database engine.Every project in this course can be tried with SQLite first, with zero setup.free
GitVersion control: tracks every change you make.Lets you compare the insecure version and the fixed version of the code.free

Step 1: see the problem and the fix in the sandbox

Reset-token table with single-use and expiry enforcementSQL: edit and run

The token itself is a long, random value generated by a secure random generator in application code (never a predictable value like a sequential id or a timestamp), bound as a parameter in every query.

Step by step

  1. Generate the reset token using a cryptographically secure random generator, long enough to resist guessing, and store it bound as a parameter.
  2. Set a short expiry (e.g. 15-30 minutes) and store expires_at alongside the token.
  3. Write the verification query checking token, used=0, and expires_at > now, all as bound parameters, in a single query.
  4. Immediately mark the token used after a successful password change, so the same token cannot be replayed.
  5. Write a test confirming a used or expired token is rejected even if it is otherwise valid.

Security checklist before you ship it

CheckWhy it mattersHow to verify
Cryptographically random tokenA guessable token defeats the whole flowConfirm the token generator uses a secure random source
Short expiry enforced in the queryLimits the window an intercepted token remains validTest an expired token is rejected
Single-use enforcementPrevents a captured token from being replayedTest a used token is rejected on a second attempt
Tip:Commit the insecure version and the fixed version as two separate commits, so the diff itself becomes a teaching example.

Quick check

1. A password reset token should be...