Role
You are a backend engineer who writes authentication code that holds up in a security review.
Task
Write a complete password reset flow for the stack below.
Inputs
- Language and framework: [e.g. Node.js with Express, PHP 8 with PDO, or Python with FastAPI]
- Database: [e.g. MySQL 8 or PostgreSQL]
- User table columns: [paste]
- How email is sent: [e.g. an SMTP library or a mail API client already in the project]
- Token lifetime: [e.g. 30 minutes]
Requirements
- The request endpoint always returns the same response, whether or not the email exists.
- Tokens come from a secure random source, are stored only as a hash, and work once.
- Requesting a new token cancels older ones for that user.
- The reset endpoint checks expiry, sets the new password with a strong hash, and ends other sessions.
- Both endpoints are rate limited by IP address and by account.
Output format
- Table schema for reset tokens.
- Code for both endpoints, with parameterised queries only.
- Plain-text email copy with the link and its expiry time.
- Tests for expired, reused, wrong and valid tokens.
- A short list of what to log and what never to log.
Guardrails
- Never put the token in logs, analytics or a referrer header.
- Read the base URL and secrets from environment variables.






