The cart, the token, and the upload: what my MERN store actually cost me

I built a full store on MongoDB, Express, React and Redux: products, an admin panel, PayPal checkout, Cloudinary uploads. These are the four places it nearly broke, and what I changed.

Articles

December 4, 2026· 7 min readBackendMongoDBExpressReactReduxPayPal

I built mern-stack-ecommerce to stop guessing about how a real store works. It has two sides: a shop with products, filters, cart, addresses and orders, and an admin panel for products, orders and image uploads. React 18 with Vite on the front, Redux Toolkit for state, Radix primitives for the UI. On the back: Express 4, Mongoose 8, JWT, bcrypt, Multer and Cloudinary.

The code is on GitHub and the demo runs on Vercel. But the feature list is not the interesting part. These are the four things that fought me.

1. Where the token lives decides your whole auth layer

I signed users in with jsonwebtoken and bcryptjs (12 rounds of hashing), and my first version kept the token in localStorage. That is the version everyone writes first, and it makes you attach the header by hand on every single request.

Moving the token into an httpOnly cookie changed the shape of the client:

  • cookie-parser on the server, and res.cookie on login instead of returning a token in the JSON body.
  • withCredentials: true on the axios calls, so the browser actually sends it.
  • CORS with the real front-end origin and credentials: true, because a wildcard origin and credentials are not allowed together.

Then a smaller problem showed up: after a refresh, the client has no idea who the user is. So there is a GET /api/auth/check-auth route behind the auth middleware that reads the cookie and returns the user, and a check-auth.jsx component that holds the protected pages while that call is in flight. Without that loading state you get a flash of the login page on every reload, and every user reads it as a bug.

2. The cart is a state problem before it is a database problem

The cart lives in a Redux slice, next to auth, products, addresses and orders. Products go in, quantities change, the total is derived. That part is easy.

The hard part is deciding where the truth lives. If the cart is only in Redux, a refresh empties it. If it is only in Mongo, every click on a quantity is a network write and the UI feels slow on a phone. I ended up with Redux as the source of truth during the session, and a write to the server when the user opens checkout, which is the moment the cart has to survive a closed tab.

Guest carts are what I would do differently. Today a guest who adds items and then logs in has two carts to reconcile, and I merged them by hand. A cart keyed to a session id and merged on login is the design I would start from.

3. Image upload has an order of operations

The admin uploads a product image: Multer takes the file, Cloudinary stores it, a URL comes back, and the product is saved with that URL.

My bug: I saved the product before the upload resolved. The product row existed with an empty image and the file was orphaned in Cloudinary. The fix is boring and correct: await the upload, take secure_url, and only then create the product. If the upload fails, nothing is written at all.

Worth saying too: Multer with memory storage plus a stream to Cloudinary keeps the file off your disk entirely. On a small VPS that matters more than it sounds.

4. The PayPal SDK I used is the old one

Checkout is wired with paypal-rest-sdk. It works, the sandbox flow runs end to end, and the order lands in Mongo when the payment succeeds.

But paypal-rest-sdk is the legacy REST API. The current integration is the Orders v2 API, and if I rebuilt this today I would use it. More important than the SDK: I would never mark an order paid from the client-side success callback alone. The server has to verify the payment before the status changes. That is the single most important line in any store, and it is the line most tutorials skip.

What I would keep

  • Radix primitives with Tailwind: accessible dialogs, selects and dropdowns I did not have to write.
  • One axios setup with the credentials flag in one place.
  • Separate route trees for admin and shop, sharing one auth check.
  • Order items that copy the title, image and price at purchase time instead of referencing the live product.

That last one is not a detail. The day a price changes, every old order still has to show what the customer actually paid. In my order model those fields are copied, which is right, but price is stored as a string. That is a bug waiting for the first discount, and it is the first thing I would fix.

The repo: mern-stack-ecommerce. Demo: mern-stack-ecommerce-sandy.vercel.app.

→ All articles