Skip to content
Back to CurriculumLesson 03 of 30
cryptography-passwordsBeginner

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.

Interactive Visual Concept ExplainerStep-by-Step Educational Simulation
Visual Step-by-Step Concept Explainercryptography-passwords
User Registration, Email Verification & Account Lifecycle Workflows
Steps:
1. User Registration
Email: alice@cosmos.app
DB Status: verified: false
Account created in pending state. Direct login restricted.
2. Crypto Token Hash
Raw Token: tok_32byte_rand
DB Store: SHA256(token)
TTL Clock: 24 Hours
Raw token is sent ONLY via email; never stored in plaintext in DB.
3. Magic Link Verification
User Click: GET /verify?t=...
DB State: pending
Token Status: Valid
Single-use enforcement stops replay attacks cold.
Step 1 of 6
Auto-stepping (1x)

Step 1: User submits email, display name, and password over HTTPS.

Security Guarantee: Always store the SHA-256 hash of verification tokens in the database, never the plaintext token.
Why This Architectural Concept Exists

Without email verification, attackers can register fake accounts using victims' emails, polluting databases, triggering account takeover vulnerabilities, or exploiting automated signup abuse.

Intuitive Mental Model

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.

Architectural System TopologyProtocol Diagram
[ 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')
CosmosApp Production Milestone

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-by-Step Execution Sequence
01

Step 1: User submits email, display name, and password over HTTPS.

02

Step 2: Server checks if the email already exists; returns a generic success response to prevent account enumeration.

03

Step 3: Server hashes the password with Argon2id and generates a 32-byte random verification token.

04

Step 4: Server hashes the verification token with SHA-256 and saves it with a 24-hour expiration.

05

Step 5: An email is dispatched with a link containing the raw verification token.

06

Step 6: User clicks the link; server hashes the submitted token and matches it against the database to activate the account.

Production Architecture Blueprint
// 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 };
}
Common Architectural Misconceptions
  • ✕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.
Common Architectural Pitfall

Reusing the same verification token across multiple clicks or failing to invalidate it upon first use.

Production Best Practice Fix

Immediately delete or set used_at = NOW() upon first successful activation.

Interactive Knowledge Check

Why should email verification tokens be hashed with SHA-256 in the database rather than stored as plaintext?