How to Implement Email Verification
Confirm a user owns an email address with a single-use, expiring token, and resend safely without enabling abuse or enumeration.
On this page
Introduction
Verifying an email address confirms that the person who signed up controls the mailbox, which is the basis for password reset and for treating the address as a channel you can trust. A verification link is a bearer credential with a long life and a wide reach, so it must be single-use, expiring, and unguessable, and the resend path must not let an attacker flood a stranger’s inbox or discover which addresses are registered. This article covers generating and storing the token, verifying it, resending safely, and avoiding the common failures.
Why Verify Email
An unverified address means the account may belong to someone who mistyped a stranger’s address, and password reset sent there would go to the wrong person. Verification confirms control of the mailbox, which is what makes the address a trusted recovery channel. It also reduces spam and abuse from accounts created with addresses the creator does not own.
Verification is not authentication: a verified address does not mean the account is legitimate, only that someone could read the mailbox. Treat it as one signal, and do not let it substitute for other controls on sensitive operations.
Generating the Token
The token must be a secure random value, long enough to be infeasible to guess, and it should be single-use and time-limited. Store only a hash of the token, so a leak of the store does not let an attacker verify arbitrary accounts, and bind the token to the account and the email address so it cannot be used for a different address.
const crypto = require('node:crypto');
function createVerificationToken(email) {
const token = crypto.randomBytes(32).toString('base64url'); // 256 bits
const hash = crypto.createHash('sha256').update(token).digest('hex');
return {
token, // sent in the email link only
hash, // stored in the database
expiresAt: new Date(Date.now() + 24 * 60 * 60 * 1000), // 24 hours
};
}
// The link carries the raw token; the database stores the hash.
function verificationLink(baseUrl, token) {
return baseUrl + '/verify-email?token=' + encodeURIComponent(token);
}
Storing and Verifying
Store one active token per account and the expiry, and mark it used when it is consumed so a second use fails. On verification, hash the presented token, look it up, check it is not used, not expired, and belongs to the account, then set the email as verified and clear the token. Every one of those checks is necessary; skipping the used check allows replay and skipping the expiry check allows an old leaked link to work.
CREATE TABLE email_verifications (
token_hash text PRIMARY KEY,
user_id uuid NOT NULL,
email text NOT NULL, -- bind the token to this address
expires_at timestamptz NOT NULL,
used_at timestamptz
);
-- Verify: mark used and set the address verified, atomically.
WITH consumed AS (
UPDATE email_verifications
SET used_at = now()
WHERE token_hash = $1 AND used_at IS NULL AND expires_at > now()
RETURNING user_id, email
)
UPDATE users u
SET email_verified = true, email = c.email
FROM consumed c
WHERE u.id = c.user_id
RETURNING u.id;
-- Zero rows means the token was unknown, used, or expired: all failures.
Resending Safely
The resend path is where abuse lives. An attacker can request a resend for a stranger’s address repeatedly to flood their inbox, or use the response to discover which addresses are registered. Rate limit resends per account and per IP, and return the same response whether or not the address exists, so the endpoint does not confirm registration.
Issuing a new token should invalidate the old one so a resend does not leave two valid links, and the resend should be idempotent within a short window so a double-click does not send two emails. Bound the total sends per address per day, so even a patient attacker cannot use the endpoint as a spam relay.
Preventing Enumeration
The signup and resend responses must not reveal whether an address is registered. If signup says “this email is already in use” it confirms the address to an attacker; if resend says “no such account” it does the same. Return a neutral message and send the appropriate email regardless, so the observable behaviour is the same for a registered and an unregistered address.
Some flows genuinely benefit from telling the user the address is taken, such as a username, but an email address is often sensitive and its registration should not be disclosed. Decide per field, and where you must not disclose, keep the response and the timing identical so neither text nor duration leaks the answer.
Common Failures
The frequent failures are: a token that is predictable or short; storing the token in plaintext; a token that does not expire; a token that is reusable; binding the token to the user but not the address, so a change of address can be verified with an old token; and a resend path with no rate limit. Each turns a verification into a vulnerability.
A subtler failure is verifying the address but not re-verifying on change: if a user changes their email, the new address must be verified again, because the old verification says nothing about the new mailbox. Treat an address change as a new verification, and do not carry the verified flag across it.
Conclusion
Email verification confirms control of a mailbox with a token that must be random, single-use, expiring, hashed at rest, and bound to both the account and the address. Verify by consuming the token atomically so a second use fails, rate limit resends per account and per IP, keep the response neutral to avoid enumeration, and re-verify whenever the address changes. Treat a verified address as one signal about the account, not as proof that the account is legitimate, and do not let it substitute for other controls on sensitive operations. Then verification is a real signal of mailbox control rather than a link that can be replayed or guessed.
Sources: OWASP: Email verification, RFC 5322: Internet message format, OWASP: Account enumeration.
FAQ
- Why store only a hash of the verification token?
- So a leak of the verification store does not let an attacker verify arbitrary accounts. The email carries the raw token and the server hashes the presented value to look it up, just as with a session token or a password.
- How do I stop the resend endpoint being used to spam?
- Rate limit resends per account and per IP, bound total sends per address per day, invalidate the old token when issuing a new one, and return the same response whether or not the address exists so it cannot be used to enumerate registered addresses.
Report a problem with this articleOpens a pre-filled issue on GitHub.