Repository navigation
feat(auth): make usernames and emails case-insensitive - #22
Conversation
Login identifiers were compared case-sensitively, so "John" and "john" were distinct accounts and a user could not log in unless they matched the exact casing used at registration. Convert users.username and users.email to the citext type (migration 165). citext compares case-insensitively while preserving the originally stored casing for display, so the existing unique constraints become case-insensitive and `WHERE username = $1` / `email = $1` lookups match regardless of case with no change to the query code itself. Also add auth.NormalizeUsername/NormalizeEmail (trim-only; case preserved), applied at the repository chokepoints (Create, Update, GetByUsername, GetByEmail) and before validation in the create paths, so surrounding whitespace no longer defeats matching or creates lookalike accounts. Verified non-destructively against the dev DB: mixed-case lookups resolve to the same row, case-variant inserts are rejected by the unique constraint, and the down migration cleanly reverts to text. Co-Authored-By: Claude Opus 4.7 <[email protected]>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughThis PR adds username and email normalization across the authentication and user management flows. Three API handlers normalize user input at request parsing. Normalization functions trim surrounding whitespace while preserving case. A database migration converts username and email columns to PostgreSQL's ChangesUsername and Email Normalization
Sequence Diagram(s)No sequence diagram is needed for this change set. The normalization flow is straightforward (request input → normalize → validate → persist) and does not involve complex multi-step interactions between distinct components that would benefit from visualization beyond what the checkpoint descriptions provide. Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
…ive-usernames feat(auth): make usernames and emails case-insensitive
feat(auth): make usernames and emails case-insensitive
Summary
users.usernameandusers.emailto PostgreSQLcitext(migration 165) so login identifiers compare case-insensitively while preserving original casing for display.auth.NormalizeUsername/NormalizeEmail(trim-only, case preserved), applied at the repository chokepoints and before validation in create paths.Problem
Usernames/emails were case-sensitive
textwith case-sensitive unique constraints, soJohnandjohnwere distinct accounts and a user could not log in unless the casing matched what they registered with.Approach & why
citextover aLOWER()functional index or storing lowercased values: it makes the existingusers_username_key/users_email_keyunique indexes case-insensitive automatically and needs no change to the lookup queries (WHERE username = $1resolves as citext), while preserving display casing.authhelper, enforced at the four repository methods (Create,Update,GetByUsername,GetByEmail) where every read/write path converges. The three create handlers (Setup, Signup, admin CreateUser) also normalize before their existing required-field checks, so whitespace-only input is rejected at the boundary.Migration / rollout
feat/audiobooksalready occupies 163/164 and the runner dedupes by version number, so 163 would have been silently skipped on the (audiobooks-lineage) dev DB and collided at merge.text(intentionally leaves thecitextextension installed).Risks / follow-up
citextcase-folding is locale-dependent for non-ASCII; ASCII identifiers are exactly case-insensitive.Test plan
go build ./...,go vet,gofmt, andgo testforinternal/auth,internal/api/handlers,internal/jellycompat— all cleanNormalizeUsername/NormalizeEmailADMIN/admin/AdMiNresolve to the same row; case-variant insert rejected byusers_username_key; down migration reverts totextAI-use disclosure
Implemented with Claude Code (Claude Opus). Design, migration, code, and dev verification were AI-assisted and human-reviewed.
Note
Medium Risk
Touches core identity storage and login matching; migration can block on pre-existing case-colliding username/email pairs when unique indexes are rebuilt.
Overview
Usernames and emails become case-insensitive for login and uniqueness while display casing is kept. Migration 165 enables PostgreSQL
citextonusers.usernameandusers.email, so existing unique indexes andWHERE username = $1/email = $1lookups compare without caring about case.New
auth.NormalizeUsernameandauth.NormalizeEmailtrim surrounding whitespace only (no lowercasing). They run inUserRepositoryon create, update, and get-by-username/email, and on setup, signup, and admin create user before required-field validation so whitespace-only identifiers fail at the API boundary.Unit tests cover the normalizers. Rollout risk: if production already has case-variant duplicates, rebuilding unique indexes can fail until those rows are merged or renamed.
Reviewed by Cursor Bugbot for commit ea2c637. Bugbot is set up for automated code reviews on this repo. Configure here.
Summary by CodeRabbit
Release Notes
New Features
Tests