# md-redwanislam/booknest architecture

> Architecture diagram of the GitHub repository md-redwanislam/booknest, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/md-redwanislam/booknest
- Repository: https://github.com/md-redwanislam/booknest

## Overview

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: [`server/routes/index.router.js`](https://github.com/md-redwanislam/booknest/blob/master/server/routes/index.router.js) (Routes), [`client/src/App.jsx`](https://github.com/md-redwanislam/booknest/blob/master/client/src/App.jsx) (App shell), [`client/src/components/api/index.js`](https://github.com/md-redwanislam/booknest/blob/master/client/src/components/api/index.js) (API client), [`server/libs/stripe.js`](https://github.com/md-redwanislam/booknest/blob/master/server/libs/stripe.js) (Integrations)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client"]
  node_client_app["App shell<br/>React app<br/>[App.jsx]"]
  node_client_layout["Layout<br/>shared frame<br/>[MainLayout.jsx]"]
  node_client_pages_home["Browse pages<br/>route screens<br/>[HomePage.jsx]"]
  node_client_pages_auth["Auth pages<br/>auth screens<br/>[LoginPage.jsx]"]
  node_client_pages_account["Account pages<br/>protected screens<br/>[CartPage.jsx]"]
  node_client_state["State providers<br/>context state<br/>[AuthProvider.jsx]"]
  node_client_hooks["Client hooks<br/>state access<br/>[useAuth.js]"]
  node_client_api["API client<br/>HTTP adapter<br/>[index.js]"]
  node_client_guard["Access guard<br/>route guard<br/>[ProtectedRoute.jsx]"]
  node_client_catalog_ui["Catalog UI<br/>book components<br/>[BookList.jsx]"]
  node_client_cart_ui["Cart & checkout<br/>commerce UI<br/>[CartList.jsx]"]
end

subgraph group_server["Server"]
  node_server_entry["Server entry<br/>Express app<br/>[index.js]"]
  node_server_routes["Routes<br/>[index.router.js]"]
  node_server_auth["Auth layer<br/>identity service<br/>[auth.controller.js]"]
  node_server_catalog["Catalog layer<br/>book service<br/>[book.controller.js]"]
  node_server_commerce["Commerce layer<br/>cart/order service<br/>[cart.controller.js]"]
  node_server_models[("Data models<br/>Mongo models<br/>[user.model.js]")]
  node_server_security["Security<br/>middleware/utils<br/>[checkAuth.js]"]
  node_server_integrations["Integrations<br/>external adapters<br/>[stripe.js]"]
  node_server_db[("Database<br/>MongoDB<br/>[db.js]")]
end

subgraph group_external["External"]
  node_external_stripe{{"Stripe<br/>payment gateway"}}
  node_external_cloudinary{{"Cloudinary<br/>media service"}}
  node_external_email{{"Email service<br/>mail delivery"}}
end

node_client_app -->|"wraps"| node_client_layout
node_client_app -->|"routes to"| node_client_pages_home
node_client_app -->|"routes to"| node_client_pages_auth
node_client_app -->|"routes to"| node_client_pages_account
node_client_layout -->|"contains"| node_client_guard
node_client_pages_home -->|"uses"| node_client_catalog_ui
node_client_pages_account -->|"uses"| node_client_cart_ui
node_client_pages_auth -->|"calls"| node_client_api
node_client_pages_home -->|"calls"| node_client_api
node_client_pages_account -->|"calls"| node_client_api
node_client_state -->|"exposes"| node_client_hooks
node_client_hooks -->|"adapts"| node_client_api
node_client_guard -->|"reads"| node_client_state
node_client_app -->|"wraps"| node_client_state
node_client_api -->|"requests"| node_server_routes
node_server_entry -->|"mounts"| node_server_routes
node_server_routes -->|"dispatches"| node_server_auth
node_server_routes -->|"dispatches"| node_server_catalog
node_server_routes -->|"dispatches"| node_server_commerce
node_server_auth -->|"uses"| node_server_security
node_server_catalog -->|"reads/writes"| node_server_models
node_server_commerce -->|"reads/writes"| node_server_models
node_server_auth -->|"reads/writes"| node_server_models
node_server_commerce -->|"uses"| node_server_integrations
node_server_integrations -->|"connects to"| node_external_stripe
node_server_integrations -->|"connects to"| node_external_cloudinary
node_server_integrations -->|"connects to"| node_external_email
node_server_entry -->|"connects to"| node_server_db
node_server_routes -.->|"orchestrates"| node_server_models

click node_client_app "https://github.com/md-redwanislam/booknest/blob/master/client/src/App.jsx"
click node_client_layout "https://github.com/md-redwanislam/booknest/blob/master/client/src/components/shared/MainLayout.jsx"
click node_client_pages_home "https://github.com/md-redwanislam/booknest/blob/master/client/src/pages/HomePage.jsx"
click node_client_pages_auth "https://github.com/md-redwanislam/booknest/blob/master/client/src/pages/LoginPage.jsx"
click node_client_pages_account "https://github.com/md-redwanislam/booknest/blob/master/client/src/pages/CartPage.jsx"
click node_client_state "https://github.com/md-redwanislam/booknest/blob/master/client/src/providers/AuthProvider.jsx"
click node_client_hooks "https://github.com/md-redwanislam/booknest/blob/master/client/src/hooks/useAuth.js"
click node_client_api "https://github.com/md-redwanislam/booknest/blob/master/client/src/components/api/index.js"
click node_client_guard "https://github.com/md-redwanislam/booknest/blob/master/client/src/routes/ProtectedRoute.jsx"
click node_client_catalog_ui "https://github.com/md-redwanislam/booknest/blob/master/client/src/components/books/BookList.jsx"
click node_client_cart_ui "https://github.com/md-redwanislam/booknest/blob/master/client/src/components/cart/CartList.jsx"
click node_server_entry "https://github.com/md-redwanislam/booknest/blob/master/server/index.js"
click node_server_routes "https://github.com/md-redwanislam/booknest/blob/master/server/routes/index.router.js"
click node_server_auth "https://github.com/md-redwanislam/booknest/blob/master/server/controllers/auth.controller.js"
click node_server_catalog "https://github.com/md-redwanislam/booknest/blob/master/server/controllers/book.controller.js"
click node_server_commerce "https://github.com/md-redwanislam/booknest/blob/master/server/controllers/cart.controller.js"
click node_server_models "https://github.com/md-redwanislam/booknest/blob/master/server/models/user.model.js"
click node_server_security "https://github.com/md-redwanislam/booknest/blob/master/server/middlewares/checkAuth.js"
click node_server_integrations "https://github.com/md-redwanislam/booknest/blob/master/server/libs/stripe.js"
click node_server_db "https://github.com/md-redwanislam/booknest/blob/master/server/configs/db.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_client_app,node_client_layout,node_client_pages_home,node_client_pages_auth,node_client_pages_account,node_client_state,node_client_hooks,node_client_api,node_client_guard,node_client_catalog_ui,node_client_cart_ui toneBlue
class node_server_entry,node_server_routes,node_server_auth,node_server_catalog,node_server_commerce,node_server_models,node_server_security,node_server_integrations,node_server_db toneAmber
class node_external_stripe,node_external_cloudinary,node_external_email toneMint
```

## Components

### Client

React/Vite frontend

- [App shell](https://github.com/md-redwanislam/booknest/blob/master/client/src/App.jsx): React app · Vite entry and top-level routing composition · `client/src/App.jsx`
- [Layout](https://github.com/md-redwanislam/booknest/blob/master/client/src/components/shared/MainLayout.jsx): shared frame · Main site chrome with navbar and footer · `client/src/components/shared/MainLayout.jsx`
- [Browse pages](https://github.com/md-redwanislam/booknest/blob/master/client/src/pages/HomePage.jsx): route screens · Home, books, single-book, contact, and error views · `client/src/pages/HomePage.jsx`
- [Auth pages](https://github.com/md-redwanislam/booknest/blob/master/client/src/pages/LoginPage.jsx): auth screens · Login and registration entry points · `client/src/pages/LoginPage.jsx`
- [Account pages](https://github.com/md-redwanislam/booknest/blob/master/client/src/pages/CartPage.jsx): protected screens · Cart, profile, wishlist, and success states · `client/src/pages/CartPage.jsx`
- [State providers](https://github.com/md-redwanislam/booknest/blob/master/client/src/providers/AuthProvider.jsx): context state · Auth, book, and cart providers that own client state · `client/src/providers/AuthProvider.jsx`
- [Client hooks](https://github.com/md-redwanislam/booknest/blob/master/client/src/hooks/useAuth.js): state access · Custom hooks for auth, books, cart, and axios wiring · `client/src/hooks/useAuth.js`
- [API client](https://github.com/md-redwanislam/booknest/blob/master/client/src/components/api/index.js): HTTP adapter · Axios/base-URL setup for backend calls · `client/src/components/api/index.js`
- [Access guard](https://github.com/md-redwanislam/booknest/blob/master/client/src/routes/ProtectedRoute.jsx): route guard · Protects authenticated routes on the client · `client/src/routes/ProtectedRoute.jsx`
- [Catalog UI](https://github.com/md-redwanislam/booknest/blob/master/client/src/components/books/BookList.jsx): book components · Book list, featured books, ratings, and pagination · `client/src/components/books/BookList.jsx`
- [Cart & checkout](https://github.com/md-redwanislam/booknest/blob/master/client/src/components/cart/CartList.jsx): commerce UI · Cart list and checkout modal · `client/src/components/cart/CartList.jsx`

### Server

Express/Mongo API

- [Server entry](https://github.com/md-redwanislam/booknest/blob/master/server/index.js): Express app · Process bootstrap and app wiring · `server/index.js`
- [Routes](https://github.com/md-redwanislam/booknest/blob/master/server/routes/index.router.js): HTTP routes · Endpoint definitions and route aggregation · `server/routes/index.router.js`
- [Auth layer](https://github.com/md-redwanislam/booknest/blob/master/server/controllers/auth.controller.js): identity service · Authentication, token flow, and account lifecycle · `server/controllers/auth.controller.js`
- [Catalog layer](https://github.com/md-redwanislam/booknest/blob/master/server/controllers/book.controller.js): book service · Book retrieval and catalog business logic · `server/controllers/book.controller.js`
- [Commerce layer](https://github.com/md-redwanislam/booknest/blob/master/server/controllers/cart.controller.js): cart/order service · Cart, wishlist, and order orchestration · `server/controllers/cart.controller.js`
- [Data models](https://github.com/md-redwanislam/booknest/blob/master/server/models/user.model.js): Mongo models · User, book, cart, order, and wishlist collections · `server/models/user.model.js`
- [Security](https://github.com/md-redwanislam/booknest/blob/master/server/middlewares/checkAuth.js): middleware/utils · Auth middleware and token helpers · `server/middlewares/checkAuth.js`
- [Integrations](https://github.com/md-redwanislam/booknest/blob/master/server/libs/stripe.js): external adapters · Stripe, Cloudinary, email, and upload helpers · `server/libs/stripe.js`
- [Database](https://github.com/md-redwanislam/booknest/blob/master/server/configs/db.js): MongoDB · Persistence connection and configuration · `server/configs/db.js`

### External

Third-party services

- **Stripe**: payment gateway · External payment processing platform
- **Cloudinary**: media service · External image storage and transformation service
- **Email service**: mail delivery · External email delivery used for verification and reset flows

## Connections

- 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
