Pattern: Account sign-up and usernames

Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.

Search terms: sign up, registration, email verification, account enumeration, username, canonical email, disposable email, race condition, rate limit

Last reviewed:

For agents

1. Problem

People ask for “an account system” without saying how sign-up should behave. Naive designs leak which emails are registered, let one mailbox create many accounts, store half-finished sign-ups with reserved names, and break when two requests arrive at once.

2. Options

Method Upside Downside Fits when
Create the account first, verify the email later Simple, fewer steps Unverified accounts reserve names and emails; spam and typos stay in the database Low-risk hobby tools
Verify the email first, create the account after (recommended) Nothing is stored for unproven emails; typos caught early One more step; needs a code or link flow Any project with real users
Email link instead of a code One tap on desktop Mail apps and link scanners may open the link and phone flows can break on app switching (practitioner experience) Web-only products
Email code (6 digits) Works in any app; link scanners cannot consume it Needs attempt limits Mobile apps, mixed web and mobile
Social sign-in only No passwords to manage Depends on a third party; some users have no such account Consumer apps with a clear audience

Sources for the trade-offs: Authentication Cheat Sheet and Email Validation and Verification Cheat Sheet.

Three steps, nothing stored for unproven input

  1. Email plus acceptance of the terms. The server sends a short-lived random code (example: 6 digits, valid 5 minutes).
  2. The user enters the code. On success the server issues a short-lived registration permit (example: 30 minutes) and stores only its hash.
  3. The user chooses username and password. The account is created in one database transaction.

Why: until step 3 there is no account, no reserved name and no stored password. A hash of the code is stored, not the code itself, so a database leak does not leak live codes.

Code handling

No existence leak

Canonical email

Race-safe final step (one transaction)

Usernames

Rate limits for sign-up (example roles, tune by traffic)

4. Scenarios and edge cases

5. Common mistakes

6. How to audit

Sources