feat: duplicate email prevention and email/name normalization#221
Open
angelospk wants to merge 1 commit intoschemalabz:mainfrom
Open
feat: duplicate email prevention and email/name normalization#221angelospk wants to merge 1 commit intoschemalabz:mainfrom
angelospk wants to merge 1 commit intoschemalabz:mainfrom
Conversation
src/app/api/admin/users/route.ts
Outdated
Comment on lines
76
to
77
| const normalizedEmail = email.trim().toLowerCase() | ||
| const normalizedName = name.trim() |
There was a problem hiding this comment.
duplicates normalization already done in the database layer (createUser calls normalizeAdminUserData)
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/app/api/admin/users/route.ts
Line: 76-77
Comment:
duplicates normalization already done in the database layer (`createUser` calls `normalizeAdminUserData`)
How can I resolve this? If you propose a fix, please make it concise.
Comment on lines
116
to
117
| const email = String(formData.get("email") ?? "").trim().toLowerCase() | ||
| const normalizedName = String(formData.get("name") ?? "").trim() |
There was a problem hiding this comment.
duplicates normalization already done in the database layer (createUser/updateUser call normalizeAdminUserData)
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/components/admin/users/user-dialog.tsx
Line: 116-117
Comment:
duplicates normalization already done in the database layer (`createUser`/`updateUser` call `normalizeAdminUserData`)
How can I resolve this? If you propose a fix, please make it concise.…licate-email handling, invite cleanup) and add test coverage - centralize email/name normalization in createUser/updateUser - prevent duplicate emails case-insensitively and map conflicts to clear API errors - improve invite flow robustness with verification-token cleanup on email failure - improve admin user dialog/API error surfacing and feedback - add route/db tests for admin users plus klitiki whitespace-normalization tests
575fd22 to
ccfa416
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #120
Description
Summary:
This Pull Request improves the reliability, security, and developer experience of the user creation and management flow. It adds comprehensive input normalization, prevents silent failures on duplicate user creation, and ensures cleaner error feedback and database state.
Changes Made:
Data Layer & Validation (
src/lib/db/users.ts):normalizeEmail,normalizeName, andnormalizeAdminUserDatafunctions. These automatically trim extra whitespaces, convert emails to lowercase, and handle empty names before they reach the database layer.createUserandupdateUserto ensure no conflicting emails exist.P2002) errors to consistently throw:"A user with this email already exists.". This fixes the root cause of the silent UI failures.Utilities (
src/lib/utils.ts):klitikiname formatting function to correctly parse, trim, and evaluate strings containing extra or trailing spaces.API Routes (
src/app/api/admin/users/route.ts):user.idinstead of the email.Frontend UI (
src/components/admin/users/user-dialog.tsx):[object Object].Testing (
src/lib/__tests__/users.test.ts,src/app/api/admin/users/route.test.ts,src/lib/__tests__/utils.test.ts):updateUser/createUsernormalization, constraints, duplicate email errors,klitikiwhitespaces, and route cleanup edge cases.Testing:
Next.jsanddb).Greptile Summary
This PR adds comprehensive input normalization and duplicate email prevention to the user management flow. The implementation normalizes emails to lowercase and trims whitespace before database operations, preventing silent failures and improving data consistency.
Key improvements:
P2002errors to user-friendly messagesklitikiutility to handle extra whitespace with regex splitTechnical highlights:
createUser/updateUser), ensuring consistencyConfidence Score: 5/5
Important Files Changed
klitikito handle whitespace properly with trim and regex splitFlowchart
%%{init: {'theme': 'neutral'}}%% flowchart TD A[User submits form] --> B[Frontend: user-dialog.tsx] B --> C[POST /api/admin/users] C --> D[Normalize email/name] D --> E{Check duplicate email<br/>case-insensitive} E -->|Exists| F[Return 409 error] E -->|Not found| G[Create user in DB] G -->|Success| H[Generate verification token] H --> I[Send invite email] I -->|Email fails| J[Delete verification token] I -->|Email succeeds| K[Keep token] J --> L[Log error, return 200] K --> L L --> M[Frontend shows toast] F --> N[Frontend shows error toast] G -->|P2002 error| O[Map to duplicate message] O --> FLast reviewed commit: ccfa416