Username or email as the login identifier? The tradeoffs
Deciding what a user types into the first field of your login form feels like a UX detail. It is actually a decision that ripples into your privacy posture, your exposure to account enumeration, your support load, and how your users think about their own accounts. Email is convenient and universal but inherently enumerable and tied to a third party. A username is private and portable but forgettable and prone to collisions. There is no free choice here, only a set of tradeoffs you should make on purpose.
The reason this matters more than it looks: the identifier is the thing an attacker needs first. Everything downstream, rate limiting, enumeration defense, reset flows, is shaped by whether your login key is a public-ish email or a chosen handle.
Email as the identifier
Using email as the primary login identifier is the default for good reasons. It is unique, users already know it, it doubles as your communication and recovery channel, and it needs no separate "pick a username" step that costs you sign-up conversions. For most SaaS products this is simply the right call.
The costs are real, though, and worth naming:
- Enumerable: email addresses are semi-public and guessable. Attackers can test whether
person@company.comhas an account and build target lists. - Privacy leakage: any feature that surfaces or confirms an email (mentions, sharing, "user already exists") can expose who uses your product.
- Mutable and external: people change jobs and email providers; the identifier you keyed on is owned by Google or a corporate IT department, not the user.
Username as the identifier
A chosen username decouples the login key from any external provider and from the user's real-world identity. It is the right choice for products where pseudonymity matters (communities, forums, social platforms) or where you do not want the login identifier to double as a contactable, enumerable address.
But usernames bring their own friction:
- Forgettable: users forget which handle they picked far more often than they forget their email, driving support tickets.
- Collisions and squatting: the good names go fast, and you inherit a namespace to police, including impersonation and reserved-word problems.
- Still needs an email anyway: you almost always need a verified email for recovery and notifications, so a username rarely removes email, it adds a field on top of it.
Rule of thumb: if your product talks to users by email and has no pseudonymity requirement, use email as the login identifier and treat usernames as optional display names. If pseudonymity is a feature, use a username for login and keep a verified email in the background purely for recovery.
The enumeration problem is about responses, not identifiers
People reach for usernames hoping to dodge account enumeration, but the identifier type is not what leaks existence, your responses are. A login form that says "no account with that email" versus "wrong password" is an enumeration oracle no matter which identifier you chose. The same goes for signup ("that email is taken"), password reset, and even response timing.
The fix is uniform responses, applied consistently across every flow.
// Enumeration-safe login: identical outcome whether or not the account exists.
async function login(identifier: string, password: string) {
const user = await findByIdentifier(identifier); // email OR username
// Always run a hash comparison, even with no user, to equalize timing.
const hash = user?.passwordHash ?? DUMMY_ARGON2_HASH;
const ok = await verifyPassword(password, hash);
if (!user || !ok) {
// One generic message for both "no such account" and "wrong password".
throw new AuthError("invalid_credentials");
}
return establishSession(user);
}
Gotcha: timing is a side channel. If you skip password hashing when the account does not exist, the "no account" path returns noticeably faster and an attacker can enumerate by stopwatch. Always perform an equivalent-cost comparison against a dummy hash so both paths take the same time.
Signup and reset leak too
Enumeration defense has to cover more than login. On signup, "that email is already registered" confirms existence; instead, accept the request and send an email that says either "here is your confirmation" or "you already have an account, here is a sign-in link," so the server's HTTP response is identical either way. On password reset, always respond "if an account exists, we sent a link," regardless of whether one does.
- Signup: resolve "already exists" over email, not in the synchronous response.
- Reset: one message whether or not the identifier matched.
- Rate limit every identifier-accepting endpoint to blunt bulk probing.
Normalization: the quiet correctness bug
Whichever identifier you choose, you must normalize it consistently at both write and read time, or you will create duplicate accounts and confusing login failures. Emails are case-insensitive in the domain and effectively so in the local part for most providers; usernames need a canonical form to prevent look-alike collisions.
-- Enforce case-insensitive uniqueness with a normalized column.
ALTER TABLE users ADD COLUMN email_normalized CITEXT
GENERATED ALWAYS AS (lower(trim(email))) STORED;
CREATE UNIQUE INDEX uq_users_email_norm ON users(email_normalized);
-- For usernames, store a folded form to block homoglyph / case squatting.
ALTER TABLE users ADD COLUMN username_canonical TEXT;
CREATE UNIQUE INDEX uq_users_username_canon ON users(username_canonical);
Be careful not to over-normalize emails: stripping dots or +tags is provider-specific behavior, and applying it universally will merge addresses that are genuinely distinct at some providers. Normalize case and whitespace reliably; treat subaddressing rules as per-provider, not universal.
You can also support both
Nothing forces a single identifier. Many mature products let users log in with either their email or a username, resolving whichever was typed to the same account. This is friendly, but it means every enumeration and normalization rule above has to hold for both inputs, and your lookup has to disambiguate cleanly. It is more surface area, chosen deliberately, not a free upgrade.
Choose on purpose, then defend it everywhere
The identifier decision is really two decisions: what the user types, and how your system refuses to confirm whether any given value exists. The first is a product and privacy call; the second is a security discipline you apply identically across login, signup, and reset no matter which identifier you picked. Owning your authentication stack is what lets you enforce that discipline end to end, normalize correctly, equalize timing, and keep your responses uniform, rather than hoping a hosted login box made the same careful choices you would have.
Run it yourself
Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.