Lorixen.
EN

Case-insensitive email logins: stop duplicate accounts

Short answer: If your app treats Aoife@Example.ie and aoife@example.ie as different addresses, customers end up with duplicate accounts, can't log in, or can't reset their password. Store the email as the customer typed it for sending, but look it up and enforce uniqueness on a normalised version, typically trimmed and lowercased. In PostgreSQL, a unique index on lower(email) or a case-insensitive collation does this; then clean up any duplicates that already exist.

How the problem shows up

The symptoms are familiar to anyone doing app support:

  • A customer signs up on their phone, which capitalised the first letter, then can't log in on their laptop.
  • Password reset says "no account found" for an address that definitely has one.
  • The same person has two accounts, two order histories and two loyalty balances.
  • A customer gets marketing email twice, once to each version of their address.

All of these come from comparing email addresses as exact strings. Phone keyboards that auto-capitalise, autofill entries saved with capitals, and people typing in a hurry make mismatches common.

What the standards say, and what to do

There's a technical wrinkle. The SMTP standard, RFC 5321, says the domain part of an address is not case sensitive, but that the local part, before the @, must be treated as case sensitive by mail software, because a host could in theory treat Smith and smith as different mailboxes. So mail servers preserve the case you send.

In practice, mainstream mailbox providers deliver to the same inbox regardless of case, and real customers expect the address to work however they typed it. The sensible approach for a business app is:

  • Keep the original as typed, and send email to it exactly, which respects the standard.
  • Compare a normalised copy for login, sign-up checks and password resets, which matches what customers expect.

That way you never change the address you send to, but you also never create a second account for the same person.

What normalisation should include

Keep it simple and predictable:

  1. Trim spaces from both ends; pasted addresses often carry a trailing space.
  2. Lowercase the whole address.

Be cautious about going further. Some providers ignore dots or anything after a + in the local part, but others don't, and stripping them could merge accounts belonging to different people. Unless you have a specific reason and know the provider's rules, don't rewrite addresses beyond trimming and lowercasing.

Enforcing it in the database

Doing the comparison in app code alone isn't enough; two sign-ups at the same moment can both pass the check. Enforce it in the database.

In PostgreSQL, a common pattern is a unique index on the lowercased value:

CREATE UNIQUE INDEX users_email_lower_key ON users (lower(email));

Queries then compare on lower(email) = lower($1), which uses the index.

PostgreSQL also offers the citext extension, a case-insensitive text type that behaves almost exactly like text but compares without regard to case. Its documentation includes a tip worth noting: consider using nondeterministic collations instead, which can provide case-insensitive and accent-insensitive comparisons and handle some Unicode cases that citext doesn't.

Other databases have their own approaches, such as case-insensitive collations or computed columns with unique indexes. The principle is the same: one rule, enforced where every write goes through it.

Clean up existing duplicates first

You can't add a unique index while duplicates exist. Before switching:

  1. Find addresses that collide once lowercased.
  2. For each group, decide which account to keep, usually the one with the most recent login or most orders.
  3. Merge or reassign orders, bookings and loyalty points to the kept account.
  4. Tell affected customers what happened and which address and password to use.
  5. Disable the other accounts rather than deleting them straight away, so you can undo a mistake.

For a small app this is a short manual job. For larger ones, script it and review the merge plan before running it.

Fix every entry point

Normalisation has to apply everywhere an email is entered or looked up:

  • Sign-up and login forms.
  • Password reset and magic-link requests.
  • Social and single sign-on logins that return an email.
  • Admin screens where staff create customers.
  • Imports from other systems, such as an old shop platform.
  • Newsletter sign-ups that link to accounts.

Missing one path is how duplicates creep back. Putting the normalisation in one shared function, and the uniqueness rule in the database, makes that much less likely.

Be careful with error messages

While you're in this code, check what your forms reveal. A sign-up error saying "this email already has an account" or a reset form saying "no account with that email" tells anyone which addresses are registered. Many apps use neutral wording instead, such as "If an account exists, we've sent a link", especially for password resets.

A worked example

Picture a hypothetical Dublin gym-membership app where members often signed up on their phones. Support regularly merged duplicate accounts by hand, and some members had paid twice for class packs under two accounts. The team found the duplicate groups, merged class credits and payments into the most active account, emailed affected members, and then added a unique index on lower(email). Sign-up, login, reset and staff screens all used one normalisation function. Duplicate-account tickets stopped.

FAQ

Should I lowercase the address I send emails to?

You don't need to. Keep the original for sending and use the normalised copy for comparison.

Does this apply to usernames too?

Usually, yes. Treating usernames case-insensitively avoids look-alike accounts and confusion.

What about addresses with accents or non-Latin characters?

They're increasingly valid. Lowercasing handles common cases, but test with real examples, and consider Unicode normalisation for full consistency.

Can I fix this without downtime?

Generally yes. Clean up duplicates, then create the index; PostgreSQL can build indexes concurrently on busy tables.

If your app keeps creating duplicate accounts, ask us to tidy up how emails are stored and matched.

Sources

WhatsAppBook a call