How md-redwanislam/booknest works
BookNest is a two-tier MERN application with a React/Vite client and an Express/MongoDB API server. The frontend is a consumer-facing bookstore; the backend owns authentication, catalog data, carts, wishlists, orders, and Stripe payment handling.
1. Frontend shell and routing
The client is organized around pages/ for route-level screens and components/ for reusable UI. MainLayout, Navbar, and Footer provide the shared frame, while routes/ProtectedRoute.jsx gates authenticated pages such as cart, profile, wishlist, and checkout success flows.
2. Client state boundaries
Global state is split into providers in providers/ for auth, books, and cart. These wrap the app and expose state/actions through custom hooks in hooks/ such as useAuth, useBook, and useCart. This keeps page components thin and centralizes state transitions.
3. Client-server communication
API access is funneled through components/api/index.js and hooks/useAxios.js, which likely configure the base URL and attach auth/refresh behavior. The client depends on VITE_BASE_URL and consumes the backend as a remote API, not via shared code.
4. Book browsing flow
The main user journey starts in HomePage and BooksPage, where the client fetches paginated lists, featured books, search results, filters, and single-book details. BookList, FeaturedBooks, Ratings, and pagination components indicate the catalog experience is read-heavy and query-driven.
5. Commerce flow on the frontend
Cart operations are handled in CartPage and CartList, with checkout presented through CheckoutModal. SuccessPage represents the post-payment landing state. The UI supports add/update/remove cart actions and then hands off to Stripe-backed checkout.
6. Auth flow on the frontend
Authentication screens are split into login and registration pages/forms. The app supports login, registration, and token-based session persistence; the README also notes automatic access-token refresh. ProtectedRoute is the main client-side access control boundary.
7. Backend layering
The server follows a clear Express layering: routes/ define HTTP endpoints, controllers/ handle request orchestration, services/ contain business logic, models/ define MongoDB collections, and middlewares/ enforce auth and other cross-cutting checks. Shared helpers live in utils/ and third-party integrations in libs/.
8. Core data model and persistence
MongoDB is the persistence layer, with models for User, Book, Cart, Order, and WishList. This implies the backend owns all durable business state, while the frontend remains a thin presentation and interaction layer.
9. Authentication and session security
Auth is JWT-based with both access and refresh tokens. The backend has dedicated auth middleware (checkAuth) plus helpers for token extraction/refresh. Email verification, OTP verification, and password reset endpoints exist, so the auth subsystem includes account lifecycle controls beyond login/logout.
10. Order and payment boundary
Stripe is isolated on the backend in libs/stripe.js, order.controller.js, and webhook.controller.js. The checkout session is created server-side, and Stripe webhooks are used to confirm payment asynchronously and finalize order state. This keeps payment verification out of the client.
11. Media and external services
Book/image uploads appear to use Multer plus Cloudinary, with dataUri helpers for transforming uploads before sending them to Cloudinary. Email delivery is handled by sendEmail.js, likely for verification/OTP flows. These are external integration points that sit behind backend services.
12. Deployment shape
Both client/vercel.json and server/vercel.json suggest separate deployment units, with the React app and API hosted independently. The architecture is therefore a decoupled SPA + API model with environment-based configuration between the two.
Overall, the main data flows are: browser UI → React state/hooks → REST API → services/models → MongoDB, and checkout → Stripe session → webhook callback → order persistence. The important architectural boundaries are frontend vs backend, authenticated vs public routes, and synchronous UI actions vs asynchronous payment confirmation.
Main entry points: index.router.js Routes · App.jsx App shell · index.js API client · stripe.js Integrations
Components 23
Client
React/Vite frontend
- App shell · Vite entry and top-level routing composition
client/src/App.jsx - Layout · Main site chrome with navbar and footer
client/src/components/shared/MainLayout.jsx - Browse pages · Home, books, single-book, contact, and error views
client/src/pages/HomePage.jsx - Auth pages · Login and registration entry points
client/src/pages/LoginPage.jsx - Account pages · Cart, profile, wishlist, and success states
client/src/pages/CartPage.jsx - State providers · Auth, book, and cart providers that own client state
client/src/providers/AuthProvider.jsx - Client hooks · Custom hooks for auth, books, cart, and axios wiring
client/src/hooks/useAuth.js - API client · Axios/base-URL setup for backend calls
client/src/components/api/index.js - Access guard · Protects authenticated routes on the client
client/src/routes/ProtectedRoute.jsx - Catalog UI · Book list, featured books, ratings, and pagination
client/src/components/books/BookList.jsx - Cart & checkout · Cart list and checkout modal
client/src/components/cart/CartList.jsx
Server
Express/Mongo API
- Server entry · Process bootstrap and app wiring
server/index.js - Routes · Endpoint definitions and route aggregation
server/routes/index.router.js - Auth layer · Authentication, token flow, and account lifecycle
server/controllers/auth.controller.js - Catalog layer · Book retrieval and catalog business logic
server/controllers/book.controller.js - Commerce layer · Cart, wishlist, and order orchestration
server/controllers/cart.controller.js - Data models · User, book, cart, order, and wishlist collections
server/models/user.model.js - Security · Auth middleware and token helpers
server/middlewares/checkAuth.js - Integrations · Stripe, Cloudinary, email, and upload helpers
server/libs/stripe.js - Database · Persistence connection and configuration
server/configs/db.js
External
Third-party services
- Stripe · External payment processing platform
- Cloudinary · External image storage and transformation service
- Email service · External email delivery used for verification and reset flows
Connections 29
- App shell → Layout: wraps · The app composes the shared frame around routed pages
- App shell → Browse pages: routes to · Public browsing screens are mounted from the app router
- App shell → Auth pages: routes to · Login and registration pages are mounted from the app router
- App shell → Account pages: routes to · Protected account flows are mounted from the app router
- Layout → Access guard: contains · Protected sections depend on the route guard
- Browse pages → Catalog UI: uses · Browse pages render catalog widgets and book lists
- Account pages → Cart & checkout: uses · Cart and checkout screens render commerce widgets
- Auth pages → API client: calls · Auth screens submit credentials through the API client
- Browse pages → API client: calls · Catalog screens fetch books and details from the API client
- Account pages → API client: calls · Commerce and profile screens use the API client
- State providers → Client hooks: exposes · Providers feed the custom hooks used by components
- Client hooks → API client: adapts · Hooks centralize network access and request behavior
- Access guard → State providers: reads · Route protection depends on auth state
- App shell → State providers: wraps · Providers supply global client state to the app tree
- API client → Routes: requests · Browser calls are sent to the backend HTTP API
- Server entry → Routes: mounts · The Express app attaches the route layer
- Routes → Auth layer: dispatches · Auth endpoints are handled by the auth layer
- Routes → Catalog layer: dispatches · Book endpoints are handled by the catalog layer
- Routes → Commerce layer: dispatches · Cart, wishlist, and order endpoints are handled here
- Auth layer → Security: uses · Authentication depends on token and access checks
- Catalog layer → Data models: reads/writes · Catalog logic persists and queries book data
- Commerce layer → Data models: reads/writes · Commerce logic persists and queries user state
- Auth layer → Data models: reads/writes · Identity data is stored in user collections
- Commerce layer → Integrations: uses · Payments, uploads, and email are invoked from commerce flows
- Integrations → Stripe: connects to · Checkout and webhook handling rely on Stripe
- Integrations → Cloudinary: connects to · Book media uploads are sent to Cloudinary
- Integrations → Email service: connects to · Verification and reset emails are sent externally
- Server entry → Database: connects to · The app bootstraps the database connection
- Routes → Data models: orchestrates · Controllers and services coordinate persistence through models