Prompt
How do I handle password reset and email verification securely?
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
To handle password reset and email verification securely, treat both flows as one-time, short-lived, hard-to-guess token problems with careful expiration, storage, and response behavior.
Core principles
-
Use cryptographically secure random tokens
- Generate tokens with a CSPRNG.
- Use at least 128 bits of entropy (more is fine).
-
Store only a hashed version of the token
- If your database leaks, raw reset/verification tokens shouldn’t be usable.
- Store
hash(token)rather than the token itself, similar to passwords.
-
Make tokens single-use and short-lived
- Password reset: typically 15–60 minutes.
- Email verification: often 24 hours or less, depending on UX.
- Invalidate tokens after successful use.
-
Bind tokens to purpose
- A reset token should only work for password resets.
- A verification token should only work for email verification.
- Include a
type/purposefield server-side.
-
Don’t reveal whether an account exists
- For “forgot password,” always respond with something like:
- “If an account exists for that email, we’ve sent instructions.”
- This prevents account enumeration.
- For “forgot password,” always respond with something like:
-
Use HTTPS everywhere
- Tokens in links must only travel over TLS.
-
Limit token reuse and scope
- One token, one action, one account.
- Rotate/invalidate any older outstanding tokens when issuing a new one, if appropriate.
-
Add rate limiting and abuse controls
- Rate limit requests by email, IP, and device fingerprint if appropriate.
- Add CAPTCHA or stronger friction for suspicious patterns.
Password reset flow
Recommended flow
- User submits email.
- Server always returns a generic success response.
- If the email exists:
- Generate a random reset token.
- Store a hash of the token with:
- user ID
- purpose =
password_reset - expiry timestamp
- used flag = false
- Send email containing a link like:
https://example.com/reset-password?token=...
- User clicks link and submits a new password.
- Server:
- Hashes incoming token
- Finds matching record
- Verifies not expired and not used
- Verifies associated account is still valid
- Updates password using a strong password hash like Argon2id, bcrypt, or scrypt
- Marks token as used
- Optionally invalidates all other sessions and reset tokens
Extra protections
- Invalidate active sessions after password change if that fits your threat model.
- Consider requiring the user to re-authenticate for sensitive changes after reset.
- Don’t allow reset tokens to be used to change email or other profile data.
Email verification flow
Recommended flow
- User signs up or changes email.
- Generate verification token.
- Store hashed token with purpose =
email_verification, expiry, used flag. - Send email with verification link.
- On click:
- Verify token and expiry
- Mark email as verified
- Mark token as used
Extra protections
- If the email address changes, treat the new email as unverified until confirmed.
- Don’t verify an email just because the account was created.
- If the user requests multiple verification emails, consider invalidating prior ones.
Token design options
Opaque token
- Random string stored hashed in DB.
- Simple and common.
- Best default choice.
Signed token
- Includes data like user ID and expiry in a signed payload.
- Can reduce DB lookups, but:
- harder to revoke
- more complex
- must be carefully designed
- For password reset and verification, opaque tokens are usually safer and simpler.
Practical implementation tips
- Use a strong password hashing algorithm for passwords:
- Argon2id preferred
- Use a strong hash like SHA-256 or HMAC-SHA-256 for token storage
- If using SHA-256, consider a server-side secret (“pepper”) or HMAC
- Normalize emails consistently before lookup:
- trim whitespace
- lowercase domain, and usually the whole address for lookup purposes
- Be careful with link scanners:
- Some email security tools may prefetch links.
- A common mitigation is to make the GET link open a confirmation page, and require a POST or second step to finalize reset/verification.
Common mistakes to avoid
- Storing reset tokens in plaintext
- Long-lived tokens
- Reusing the same token for multiple purposes
- Returning “email not found” on forgot-password
- Putting sensitive actions on GET without a follow-up confirmation step
- Logging tokens in application logs or analytics
- Using weak randomness like
Math.random() - Not invalidating sessions after a password reset
A good secure baseline
- Random 32-byte token
- Hash token before storing
- 15–30 minute reset expiry
- 24-hour verification expiry
- One-time use
- Generic user-facing responses
- HTTPS
- Rate limiting
- Argon2id password hashing
- Session invalidation after reset
If you want, I can also give you:
- a reference database schema,
- example code in Node/Python/Go, or
- a full secure flow diagram for both features.
Brands