The challenge
The store started as a static HTML site on a Supabase backend. Prices, order totals and protected-page checks all ran in browser JavaScript, which anyone can read and change, and the account pages rendered first and redirected after.
Its "Summer Sale" badges were synthetic too: each item's discount percentage was generated from a seed, not compared with any price the store had actually charged.
The approach
I rebuilt the source from a mirror of the live site, then moved every rule that matters off the client. Checkout now sends only item codes and quantities. A place-order Edge Function re-prices each line from a server price map plus live catalog overrides, and a payment webhook verifies its HMAC signature before a database function marks the order paid and credits the referral, each exactly once.
Pricing lives in one plain module that both the site and the server price map are built from. New prices are blended with the previous list price, charm-rounded in integer cents so float noise can't bump a price up a step, and a strikethrough "was" price only appears where the item really sold for more before. Then I ported the whole thing to the Next.js App Router, with middleware that redirects signed-out visitors before a protected page renders.
- Server-side pricingClient totals are ignored; the checkout function recomputes every line and rejects inactive or out-of-stock items.
- Honest "was" pricesA markdown shows only against a real prior list price, never a made-up one.
- Affiliate programApplications, referral codes, payout requests and a payout ledger, all behind Row Level Security.
- Owner dashboardEleven tabs for analytics, orders, affiliates, messages, catalog prices and stock, certificates and photos.
How an order is priced
- 01BrowsePrices rendered from the shared catalog module
- 02CartLines re-anchor to live prices and stock
- 03place-orderServer re-prices and stores the order
- 04WebhookSignature checked, paid once, referral once



