Skip to content

fix(auth): dynamically resolve field-specific conflicts from MongoDB E11000 duplicate keys#680

Open
Aryan0819 wants to merge 2 commits into
GitMetricsLab:mainfrom
Aryan0819:singn
Open

fix(auth): dynamically resolve field-specific conflicts from MongoDB E11000 duplicate keys#680
Aryan0819 wants to merge 2 commits into
GitMetricsLab:mainfrom
Aryan0819:singn

Conversation

@Aryan0819
Copy link
Copy Markdown
Contributor

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

  • Bug fix
  • New feature
  • Code style update
  • Breaking change
  • Documentation update

Aryan0819 added 2 commits June 2, 2026 18:50
Add session middleware with cookie settings
Updated error handling for user signup to provide specific validation messages for duplicate fields.
@netlify
Copy link
Copy Markdown

netlify Bot commented Jun 2, 2026

Deploy Preview for github-spy failed.

Name Link
🔨 Latest commit 3a8039f
🔍 Latest deploy log https://app.netlify.com/projects/github-spy/deploys/6a1edbd91a6cb90008c41de9

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 2, 2026

Warning

Review limit reached

@Aryan0819, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2f939dab-db05-4cc6-87fc-bfcfd26631b4

📥 Commits

Reviewing files that changed from the base of the PR and between 53f820b and 3a8039f.

📒 Files selected for processing (2)
  • backend/routes/auth.js
  • backend/server.js
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 Bug Report: Unhandled Validation Errors / Server Crash on Duplicate Email

1 participant