fix(wishlist): scope wishlist to the authenticated user#525
Open
sricharan12-hub wants to merge 1 commit into
Open
fix(wishlist): scope wishlist to the authenticated user#525sricharan12-hub wants to merge 1 commit into
sricharan12-hub wants to merge 1 commit into
Conversation
The wishlist controller used an unbounded Wishlist.findOne(), so every user shared a single global wishlist — User A's additions were visible to User B, a data-integrity and privacy bug (niharika-mente#508). - Add a required, unique `user` reference to the Wishlist model so each user has exactly one wishlist - Require authentication (protect) on all wishlist routes; these were previously unauthenticated, so req.user did not exist - Scope every query to req.user._id and use atomic findOneAndUpdate with upsert + $addToSet / $pull (lazy create, no duplicates, no races) - Validate productId (400 on missing/invalid, 404 on unknown product) - Add tests covering auth, validation, de-duplication and cross-user isolation (89% controller / 100% model statement coverage) Closes niharika-mente#508
|
@sricharan12-hub is attempting to deploy a commit to the niharika-mente's projects Team on Vercel. A member of the Team first needs to authorize it. |
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.
Summary
Fixes #508 — the wishlist was shared globally across every user.
getWishlist,addToWishlist, andremoveFromWishlistall ran an unboundedWishlist.findOne(), so there was a single wishlist document for the whole platform. If User A added a product, User B saw it too — a data-integrity and privacy failure.Root cause
Two problems compounded each other:
Wishlistmodel had nouserfield, so wishlists couldn't be distinguished per user.// ✅ No authentication middleware), soreq.userdidn't even exist — the suggestedfindOne({ userId: req.user._id })fix couldn't work as-is.Changes
Wishlist.model.js): added auserreference that isrequiredandunique, guaranteeing exactly one wishlist per user.wishlist.route.js): applied the existingprotectmiddleware (same pattern as orders/returns) to all three routes.WishlistController.controller.js):req.user._id.add/removeuse atomicfindOneAndUpdatewithupsert+$addToSet/$pull— lazy-creates the wishlist, prevents duplicate entries, and avoids create races.productId→400for missing/invalid ids,404for unknown products.The frontend already sends the auth token on every request (shared axios interceptor), so the logged-in UI is unaffected; logged-out requests now correctly receive
401instead of a shared wishlist.Tests
New
tests/wishlist.test.js(supertest + mongodb-memory-server) — 15/15 passing, covering authentication, validation, de-duplication, graceful no-op removal, and most importantly cross-user isolation (User A's wishlist is invisible to User B; one user's removal never affects another).Coverage on the new logic: 89.28% controller / 100% model statements — above the 80% requirement.
Closes #508