# anmolmehra435/achat architecture

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

- Interactive diagram: https://gitdiagram.com/anmolmehra435/achat
- Repository: https://github.com/anmolmehra435/achat

## Overview

1. This repository is a two-tier MERN application: a React single-page app in `frontend/` talks to an Express/Node REST API in `backend/`, with MongoDB as the primary database and Cloudinary as the external media store.

2. The backend is organized in a classic MVC split. `server.js` is the composition root that wires configuration, middleware, routes, and the database connection. Business logic lives in `controllers/`, persistence schemas in `models/`, and HTTP endpoint definitions in `routes/`.

3. Authentication is a first-class subsystem. `authController.js` and `authRoutes.js` handle register/login/refresh flows, while `verifyAccessJwt.js` is the main protection boundary for authenticated APIs. The user model stores both `refreshToken` and profile data, which implies token refresh is tied to the persisted user record.

4. User management is separate from auth. `userController.js` and `userRoutes.js` expose endpoints for fetching the current user, updating the profile, and searching users. The `users` model includes `name`, `username`, `email`, `password`, `avatar`, and `refreshToken`, so the user document is both identity and session anchor.

5. Profile image uploads are handled as a dedicated pipeline: the frontend sends multipart form data, `middleware/multer.js` processes the upload, and `config/cloudinary.js` pushes the image to Cloudinary. The resulting Cloudinary URL is then stored back in MongoDB and rendered by the frontend.

6. Conversations are modeled in MongoDB using references rather than embedded user snapshots. `conversation.js` stores a `members` array of user ObjectIds, and `conversationController.js`/`conversationRoutes.js` implement create-and-list behavior. The README indicates duplicate conversation prevention and `populate`-based member hydration, so the conversation layer is designed around relationship lookup rather than denormalized chat rooms.

7. Message-related files exist in the backend (`messagesController.js`, `messagesRoutes.js`, `messages.js`), but the README still describes realtime messaging as upcoming. For architecture purposes, treat this as a partially scaffolded or evolving subsystem: the current codebase is centered on auth, profiles, and conversation management, with realtime delivery not yet the primary runtime path.

8. The frontend is a Vite-built React app with a small page-oriented structure: `Login.jsx`, `Dashboard.jsx`, and `EditProfile.jsx`. `App.jsx` is the top-level router/shell, while page-specific styling lives in dedicated CSS files. This suggests a route-driven UI rather than a heavily componentized design.

9. Frontend data access is split into service modules under `src/services/`. `authServices.js` and `verifyJwtService.js` centralize API calls related to login/session validation, which keeps token and auth checks out of the page components. The dashboard and profile pages likely consume these services to fetch user state and protected resources.

10. The main runtime data flow is: login on the frontend → backend validates credentials and issues tokens → client stores tokens and uses protected APIs → `verifyAccessJwt` guards server routes → user/dashboard calls fetch current profile and conversations. This is the key boundary for any diagram: browser UI on one side, authenticated REST API on the other.

11. The profile update flow is a second important path: user edits profile in React → multipart request to backend → Multer/Cloudinary upload path → MongoDB update → refreshed avatar URL displayed in the UI. That flow crosses the frontend, Express middleware, cloud storage, and database layers.

12. Infrastructure-wise, the repo depends on MongoDB, Cloudinary, JWT secrets, and standard Node/React tooling. Vite, Axios, React Router DOM, Express, Mongoose, and Multer are the materially relevant technologies; there is no evidence in the current tree of Socket.IO, background jobs, or separate microservices.

Main entry points: [`backend/server.js`](https://github.com/anmolmehra435/achat/blob/main/backend/server.js) (Server), [`backend/models/users.js`](https://github.com/anmolmehra435/achat/blob/main/backend/models/users.js) (Users model), [`backend/controllers/userController.js`](https://github.com/anmolmehra435/achat/blob/main/backend/controllers/userController.js) (User controller), [`backend/routes/authRoutes.js`](https://github.com/anmolmehra435/achat/blob/main/backend/routes/authRoutes.js) (Auth routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App shell<br/>React router<br/>[App.jsx]"]
  node_frontend_login["Login<br/>page<br/>[Login.jsx]"]
  node_frontend_dashboard["Dashboard<br/>page<br/>[Dashboard.jsx]"]
  node_frontend_edit_profile["Edit profile<br/>page<br/>[EditProfile.jsx]"]
  node_frontend_auth_services["Auth service<br/>api client<br/>[authServices.js]"]
  node_frontend_verify_service["JWT check<br/>api client"]
end

subgraph group_backend["Backend"]
  node_backend_server["Server<br/>Express entry<br/>[server.js]"]
  node_backend_auth_routes["Auth routes<br/>Express routes<br/>[authRoutes.js]"]
  node_backend_auth_controller["Auth controller<br/>[authController.js]"]
  node_backend_verify_jwt["Access guard<br/>middleware<br/>[verifyAccessJwt.js]"]
  node_backend_user_routes["User routes<br/>Express routes<br/>[userRoutes.js]"]
  node_backend_user_controller["User controller<br/>[userController.js]"]
  node_backend_conversation_routes["Conversation routes<br/>Express routes"]
  node_backend_conversation_controller["Conversation controller"]
  node_backend_messages_routes["Message routes<br/>Express routes<br/>[messagesRoutes.js]"]
  node_backend_messages_controller["Messages controller"]
  node_backend_multer["Upload middleware<br/>[multer.js]"]
  node_backend_cloudinary["Cloudinary config<br/>integration<br/>[cloudinary.js]"]
  node_backend_users_model[("Users model<br/>mongoose model<br/>[users.js]")]
  node_backend_conversation_model[("Conversation model<br/>mongoose model<br/>[conversation.js]")]
  node_backend_messages_model[("Messages model<br/>mongoose model<br/>[messages.js]")]
end

subgraph group_external["External services"]
  node_external_mongo[("MongoDB<br/>database")]
  node_external_cloudinary[("Cloudinary<br/>media store")]
end

node_frontend_app -->|"routes to"| node_frontend_login
node_frontend_app -->|"routes to"| node_frontend_dashboard
node_frontend_app -->|"routes to"| node_frontend_edit_profile
node_frontend_login -->|"uses"| node_frontend_auth_services
node_frontend_dashboard -->|"uses"| node_frontend_verify_service
node_frontend_dashboard -->|"calls"| node_backend_user_routes
node_frontend_edit_profile -->|"calls"| node_backend_user_routes
node_frontend_auth_services -->|"calls"| node_backend_auth_routes
node_frontend_verify_service -->|"calls"| node_backend_auth_routes
node_backend_server -->|"mounts"| node_backend_auth_routes
node_backend_server -->|"mounts"| node_backend_user_routes
node_backend_server -->|"mounts"| node_backend_conversation_routes
node_backend_server -->|"mounts"| node_backend_messages_routes
node_backend_server -->|"applies"| node_backend_verify_jwt
node_backend_server -->|"applies"| node_backend_multer
node_backend_auth_routes -->|"delegates to"| node_backend_auth_controller
node_backend_user_routes -->|"delegates to"| node_backend_user_controller
node_backend_conversation_routes -->|"delegates to"| node_backend_conversation_controller
node_backend_messages_routes -->|"delegates to"| node_backend_messages_controller
node_backend_auth_controller -->|"reads/writes"| node_backend_users_model
node_backend_user_controller -->|"reads/writes"| node_backend_users_model
node_backend_conversation_controller -->|"reads/writes"| node_backend_conversation_model
node_backend_messages_controller -->|"reads/writes"| node_backend_messages_model
node_backend_users_model -->|"persists to"| node_external_mongo
node_backend_conversation_model -->|"persists to"| node_external_mongo
node_backend_messages_model -->|"persists to"| node_external_mongo
node_backend_multer -->|"uploads to"| node_backend_cloudinary
node_backend_cloudinary -->|"stores in"| node_external_cloudinary
node_backend_user_controller -->|"uses"| node_backend_multer
node_backend_user_controller -->|"uses"| node_backend_cloudinary
node_backend_verify_jwt -->|"validates against"| node_backend_users_model
node_backend_conversation_controller -->|"references"| node_backend_users_model

click node_frontend_app "https://github.com/anmolmehra435/achat/blob/main/frontend/src/App.jsx"
click node_frontend_login "https://github.com/anmolmehra435/achat/blob/main/frontend/src/pages/Login.jsx"
click node_frontend_dashboard "https://github.com/anmolmehra435/achat/blob/main/frontend/src/pages/Dashboard.jsx"
click node_frontend_edit_profile "https://github.com/anmolmehra435/achat/blob/main/frontend/src/pages/EditProfile.jsx"
click node_frontend_auth_services "https://github.com/anmolmehra435/achat/blob/main/frontend/src/services/authServices.js"
click node_frontend_verify_service "https://github.com/anmolmehra435/achat/blob/main/frontend/src/services/verifyJwtService.js"
click node_backend_server "https://github.com/anmolmehra435/achat/blob/main/backend/server.js"
click node_backend_auth_routes "https://github.com/anmolmehra435/achat/blob/main/backend/routes/authRoutes.js"
click node_backend_auth_controller "https://github.com/anmolmehra435/achat/blob/main/backend/controllers/authController.js"
click node_backend_verify_jwt "https://github.com/anmolmehra435/achat/blob/main/backend/middleware/verifyAccessJwt.js"
click node_backend_user_routes "https://github.com/anmolmehra435/achat/blob/main/backend/routes/userRoutes.js"
click node_backend_user_controller "https://github.com/anmolmehra435/achat/blob/main/backend/controllers/userController.js"
click node_backend_conversation_routes "https://github.com/anmolmehra435/achat/blob/main/backend/routes/conversationRoutes.js"
click node_backend_conversation_controller "https://github.com/anmolmehra435/achat/blob/main/backend/controllers/conversationController.js"
click node_backend_messages_routes "https://github.com/anmolmehra435/achat/blob/main/backend/routes/messagesRoutes.js"
click node_backend_messages_controller "https://github.com/anmolmehra435/achat/blob/main/backend/controllers/messagesController.js"
click node_backend_multer "https://github.com/anmolmehra435/achat/blob/main/backend/middleware/multer.js"
click node_backend_cloudinary "https://github.com/anmolmehra435/achat/blob/main/backend/config/cloudinary.js"
click node_backend_users_model "https://github.com/anmolmehra435/achat/blob/main/backend/models/users.js"
click node_backend_conversation_model "https://github.com/anmolmehra435/achat/blob/main/backend/models/conversation.js"
click node_backend_messages_model "https://github.com/anmolmehra435/achat/blob/main/backend/models/messages.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_frontend_app,node_frontend_login,node_frontend_dashboard,node_frontend_edit_profile,node_frontend_auth_services,node_frontend_verify_service toneBlue
class node_backend_server,node_backend_auth_routes,node_backend_auth_controller,node_backend_verify_jwt,node_backend_user_routes,node_backend_user_controller,node_backend_conversation_routes,node_backend_conversation_controller,node_backend_messages_routes,node_backend_messages_controller,node_backend_multer,node_backend_cloudinary,node_backend_users_model,node_backend_conversation_model,node_backend_messages_model toneAmber
class node_external_mongo,node_external_cloudinary toneMint
```

## Components

### Frontend

React/Vite SPA and client-side service layer

- [App shell](https://github.com/anmolmehra435/achat/blob/main/frontend/src/App.jsx): React router · Top-level SPA router and layout · `frontend/src/App.jsx`
- [Login](https://github.com/anmolmehra435/achat/blob/main/frontend/src/pages/Login.jsx): page · Authentication entry screen · `frontend/src/pages/Login.jsx`
- [Dashboard](https://github.com/anmolmehra435/achat/blob/main/frontend/src/pages/Dashboard.jsx): page · Main authenticated workspace · `frontend/src/pages/Dashboard.jsx`
- [Edit profile](https://github.com/anmolmehra435/achat/blob/main/frontend/src/pages/EditProfile.jsx): page · Profile update screen · `frontend/src/pages/EditProfile.jsx`
- [Auth service](https://github.com/anmolmehra435/achat/blob/main/frontend/src/services/authServices.js): api client · Client calls for login and session flows · `frontend/src/services/authServices.js`
- [JWT check](https://github.com/anmolmehra435/achat/blob/main/frontend/src/services/verifyJwtService.js): api client · Client-side protected-session validation · `frontend/src/services/verifyJwtService.js`

### Backend

Express API, middleware, controllers, and models

- [Server](https://github.com/anmolmehra435/achat/blob/main/backend/server.js): Express entry · Composition root for config, middleware, routes, and DB · `backend/server.js`
- [Auth routes](https://github.com/anmolmehra435/achat/blob/main/backend/routes/authRoutes.js): Express routes · Register, login, and refresh endpoints · `backend/routes/authRoutes.js`
- [Auth controller](https://github.com/anmolmehra435/achat/blob/main/backend/controllers/authController.js): controller · Authentication and token flow logic · `backend/controllers/authController.js`
- [Access guard](https://github.com/anmolmehra435/achat/blob/main/backend/middleware/verifyAccessJwt.js): middleware · Protects authenticated API routes · `backend/middleware/verifyAccessJwt.js`
- [User routes](https://github.com/anmolmehra435/achat/blob/main/backend/routes/userRoutes.js): Express routes · Current user, profile update, and search endpoints · `backend/routes/userRoutes.js`
- [User controller](https://github.com/anmolmehra435/achat/blob/main/backend/controllers/userController.js): controller · User profile and lookup logic · `backend/controllers/userController.js`
- [Conversation routes](https://github.com/anmolmehra435/achat/blob/main/backend/routes/conversationRoutes.js): Express routes · Create and list conversation endpoints · `backend/routes/conversationRoutes.js`
- [Conversation controller](https://github.com/anmolmehra435/achat/blob/main/backend/controllers/conversationController.js): controller · Conversation creation and hydration logic · `backend/controllers/conversationController.js`
- [Message routes](https://github.com/anmolmehra435/achat/blob/main/backend/routes/messagesRoutes.js): Express routes · Messaging endpoints scaffold · `backend/routes/messagesRoutes.js`
- [Messages controller](https://github.com/anmolmehra435/achat/blob/main/backend/controllers/messagesController.js): controller · Partial messaging subsystem · `backend/controllers/messagesController.js`
- [Upload middleware](https://github.com/anmolmehra435/achat/blob/main/backend/middleware/multer.js): middleware · Multipart parsing for profile images · `backend/middleware/multer.js`
- [Cloudinary config](https://github.com/anmolmehra435/achat/blob/main/backend/config/cloudinary.js): integration · Uploads media to Cloudinary · `backend/config/cloudinary.js`
- [Users model](https://github.com/anmolmehra435/achat/blob/main/backend/models/users.js): mongoose model · Identity and session anchor document · `backend/models/users.js`
- [Conversation model](https://github.com/anmolmehra435/achat/blob/main/backend/models/conversation.js): mongoose model · Member reference-based chat document · `backend/models/conversation.js`
- [Messages model](https://github.com/anmolmehra435/achat/blob/main/backend/models/messages.js): mongoose model · Message persistence schema · `backend/models/messages.js`

### External services

Datastores and third-party media storage

- **MongoDB**: database · Primary data store
- **Cloudinary**: media store · External image storage

## Connections

- App shell → Login: routes to · Public auth entry
- App shell → Dashboard: routes to · Authenticated workspace
- App shell → Edit profile: routes to · Profile editor screen
- Login → Auth service: uses · Submit credentials and session actions
- Dashboard → JWT check: uses · Checks session and protected access
- Dashboard → User routes: calls · Fetch current user and conversations
- Edit profile → User routes: calls · Submit profile updates
- Auth service → Auth routes: calls · Login and refresh requests
- JWT check → Auth routes: calls · Session validation requests
- Server → Auth routes: mounts · Registers auth endpoints
- Server → User routes: mounts · Registers user endpoints
- Server → Conversation routes: mounts · Registers conversation endpoints
- Server → Message routes: mounts · Registers messaging endpoints
- Server → Access guard: applies · Protects authenticated routes
- Server → Upload middleware: applies · Handles multipart uploads
- Auth routes → Auth controller: delegates to · Auth request handling
- User routes → User controller: delegates to · Profile and lookup handling
- Conversation routes → Conversation controller: delegates to · Conversation handling
- Message routes → Messages controller: delegates to · Message handling scaffold
- Auth controller → Users model: reads/writes · Stores users and refresh tokens
- User controller → Users model: reads/writes · Profile updates and user search
- Conversation controller → Conversation model: reads/writes · Creates and lists conversations
- Messages controller → Messages model: reads/writes · Message persistence path
- Users model → MongoDB: persists to · MongoDB-backed collection
- Conversation model → MongoDB: persists to · MongoDB-backed collection
- Messages model → MongoDB: persists to · MongoDB-backed collection
- Upload middleware → Cloudinary config: uploads to · Profile image media pipeline
- Cloudinary config → Cloudinary: stores in · External media storage
- User controller → Upload middleware: uses · Profile image upload flow
- User controller → Cloudinary config: uses · Persists avatar URLs
- Access guard → Users model: validates against · Token/session tied to user record
- Conversation controller → Users model: references · Conversation members are user ids
