User Registration, Email Verification & Account Lifecycle Workflows
A secure registration workflow validates email format, enforces password entropy rules, prevents user enumeration, and issues single-use cryptographically random verification tokens that expire quickly.
Step 1: User submits email, display name, and password over HTTPS.
Without email verification, attackers can register fake accounts using victims' emails, polluting databases, triggering account takeover vulnerabilities, or exploiting automated signup abuse.
The Certified Mail Receipt: When opening a bank account online, the bank doesn't activate wire transfers until you enter the secret activation code mailed to your physical address, proving you genuinely control that mailbox.
[ Client ] ──(POST /register)──► [ CosmosApp API ]
│
┌─────────────┴─────────────┐
▼ ▼
[ Generate 32-byte Token ] [ Save User (status: 'pending') ]
[ SHA-256 Hash Token ]
[ Store Hash in DB (24h) ]
│
▼ (Send Email Link)
[ User Mailbox: /verify?token=rawToken ]
│
▼ (GET /verify?token=rawToken)
[ CosmosApp: Verify SHA256(rawToken) == db.hash ]
│
▼ (Activate User: status = 'active')Alice registers for CosmosApp with alice@cosmos.io. CosmosApp generates a 32-byte crypto random string, hashes it with SHA-256 before storing it in PostgreSQL with an expiration timestamp of 24 hours, and emails the plaintext link to Alice. Storing the hash in the database ensures that a leaked DB backup does not allow attackers to verify pending accounts.
Step 1: User submits email, display name, and password over HTTPS.
Step 2: Server checks if the email already exists; returns a generic success response to prevent account enumeration.
Step 3: Server hashes the password with Argon2id and generates a 32-byte random verification token.
Step 4: Server hashes the verification token with SHA-256 and saves it with a 24-hour expiration.
Step 5: An email is dispatched with a link containing the raw verification token.
Step 6: User clicks the link; server hashes the submitted token and matches it against the database to activate the account.
// CosmosApp Email Verification Token Generation:
import crypto from 'node:crypto';
export function createVerificationToken() {
// 1. High-entropy random token sent to the user:
const rawToken = crypto.randomBytes(32).toString('hex');
// 2. Stored hash saved in the database:
const tokenHash = crypto.createHash('sha256').update(rawToken).digest('hex');
const expiresAt = new Date(Date.now() + 24 * 60 * 60 * 1000); // 24 hours
return { rawToken, tokenHash, expiresAt };
}- ✕Storing verification tokens in plaintext in the database is safe (If the DB leaks, attackers can verify all accounts and take over unverified addresses).
- ✕Displaying 'Email already registered' is good UX (It enables attackers to enumerate whether high-profile targets hold accounts on the platform).
Key Architectural Takeaways
- ✓Always store the SHA-256 hash of verification tokens in the database, never the plaintext token.
- ✓Set strict expiration windows (15 minutes to 24 hours) and ensure tokens are strictly single-use.
- ✓Adopt anti-enumeration responses on registration endpoints to prevent user harvesting.
Reusing the same verification token across multiple clicks or failing to invalidate it upon first use.
Immediately delete or set used_at = NOW() upon first successful activation.
Why should email verification tokens be hashed with SHA-256 in the database rather than stored as plaintext?