Great question — answering as the UphillTech team:
Maybe... just ditch cart functionality for unregistered users—sometimes, less is more. Remove cart functionality for unregistered users – Sometimes, less is more. Eliminate cart merging in fetchCart to prevent quantity duplication issues. Ensure guest users only use localStorage for their cart, keeping it separate from authenticated users. For registered users, load the cart from MongoDB and overwrite the cart state in useState . (Optional) Display a message encouraging guest users to register so they can save their cart.
Full answer by 0Veritas0: https://stackoverflow.stackexchange.com/a/79438181 (CC BY-SA 4.0).
Need it done for your business? UphillTech.