fix(auth): dynamically resolve field-specific conflicts from MongoDB E11000 duplicate keys#680
fix(auth): dynamically resolve field-specific conflicts from MongoDB E11000 duplicate keys#680Aryan0819 wants to merge 2 commits into
Conversation
Add session middleware with cookie settings
Updated error handling for user signup to provide specific validation messages for duplicate fields.
❌ Deploy Preview for github-spy failed.
|
|
Warning Review limit reached
More reviews will be available in 48 minutes and 47 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Related Issue
Description
🧱 The Architectural Context:
When schemas enforce unique data parameters (like unique: true on an email property), MongoDB creates an indexed constraint network to preserve data integrity across collections.
❌ The Failure Mechanism:
If a new user attempts to sign up with an email address or username that already exists, the database rejects the insertion block and throws a raw internal network error: MongoServerError: E11000 duplicate key error. Because our controller logic lacked an error filter to isolate this specific code, Express would halt route execution immediately upon rejection.
💥 The Impact:
The application layer defaults to an unformatted 500 Internal Server Error payload. This breaks user navigation on the frontend UI with a generic error indicator and leaks raw, sensitive system database architectures into production application log streams.
✅ The Solution:
Implemented an indexed error pattern interceptor directly inside the authentication storage block's catch parameters. The filter captures error code 11000, overrides the default server failure sequence, and dynamically extracts the conflicting property using Object.keys(err.keyValue)[0] to send a precise, customer-facing 400 Bad Request validation message back down the client network.
How Has This Been Tested?
Duplicate Registration Rejection: Attempted creating a second profile using an email address already registered in the system. Verified that the API instantly rejects the submission with a 400 status and returns the clear message string.
Fresh Account Integrity: Confirmed that signing up with a unique, unregistered email address continues to bypass the error block and commits cleanly to MongoDB.
Type of Change