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.
How to think about this
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.
| Level | Time needed |
|---|---|
| Intermediate | 3-4 hours |
What you need (tools and setup)
| Tool | What it is | Why you need it | Cost |
|---|---|---|---|
| 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 |
| Git | Version 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
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
- Generate the reset token using a cryptographically secure random generator, long enough to resist guessing, and store it bound as a parameter.
- Set a short expiry (e.g. 15-30 minutes) and store expires_at alongside the token.
- Write the verification query checking token, used=0, and expires_at > now, all as bound parameters, in a single query.
- Immediately mark the token used after a successful password change, so the same token cannot be replayed.
- Write a test confirming a used or expired token is rejected even if it is otherwise valid.
Security checklist before you ship it
| Check | Why it matters | How to verify |
|---|---|---|
| Cryptographically random token | A guessable token defeats the whole flow | Confirm the token generator uses a secure random source |
| Short expiry enforced in the query | Limits the window an intercepted token remains valid | Test an expired token is rejected |
| Single-use enforcement | Prevents a captured token from being replayed | Test a used token is rejected on a second attempt |
Quick check
1. A password reset token should be...
Randomness, expiry, and single-use together make the token safe.