Ask any question about Website Security here... and get an instant response.
Post this Question & Answer:
How do I securely implement password reset functionality on my website?
Asked on Feb 18, 2026
Answer
To securely implement password reset functionality, ensure you use token-based reset links sent to the user's email, and verify the token's validity before allowing a password change.
<!-- BEGIN COPY / PASTE -->
// Generate a secure token
const crypto = require('crypto');
const resetToken = crypto.randomBytes(32).toString('hex');
// Store hash of the token in the database
const hashedToken = crypto.createHash('sha256').update(resetToken).digest('hex');
await User.update({ passwordResetToken: hashedToken, tokenExpiry: Date.now() + 3600000 });
// Send email with reset link
const resetLink = `https://yourdomain.com/reset-password?token=${resetToken}&email=${userEmail}`;
sendEmail(userEmail, 'Password Reset', `Click here to reset your password: ${resetLink}`);
<!-- END COPY / PASTE -->Additional Comment:
- Always hash the token before storing it in the database to prevent exposure if the database is compromised.
- Set an expiration time for the token (e.g., 1 hour) to limit the window of opportunity for misuse.
- Ensure the reset link is sent over HTTPS to prevent interception by attackers.
✅ Answered with Security best practices.
Recommended Links:
