# alokranjan89/nightshield architecture

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

- Interactive diagram: https://gitdiagram.com/alokranjan89/nightshield
- Repository: https://github.com/alokranjan89/nightshield

## Overview

#### Purpose and Runtime Topology
NightShield is a browser-based personal-safety system: users or guests create SOS sessions with location, and connected nearby users receive real-time alerts. The React/Vite client runs separately from a Node/Express API with Socket.IO. MongoDB persists users, contacts, and SOS state; Cloudinary stores evidence.

- Client bootstrap: `client/src/main.jsx`, `client/src/App.jsx`
- Server bootstrap: `server/server.js`, `server/src/app.js`
- Deployment targets: `client/vercel.json`, `render.yaml`

#### Client Application and User Flows
React Router pages implement the primary workflows: public home/login, dashboard, contact management, active SOS handling, history/status, and settings. Shared UI includes the navigation, SOS control, alerts, and status display.

- Workflow pages: `client/src/pages/Home.jsx`, `client/src/pages/Dashboard.jsx`, `client/src/pages/SOSActive.jsx`
- Shared emergency UI: `client/src/components/SOSButton.jsx`, `client/src/components/AlertPopup.jsx`, `client/src/components/StatusIndicator.jsx`

#### SOS Orchestration Boundary
The client’s SOS domain is centralized around context and hooks. A press-and-hold interaction triggers creation; browser geolocation is collected where permitted; client state drives the active SOS screen and later resolution/evidence behavior. REST is used for durable commands and history, while sockets deliver immediate peer notifications.

- State/control layer: `client/src/context/SOSContext.jsx`, `client/src/hooks/useSOS.js`, `client/src/hooks/useHold.js`
- Location capture: `client/src/hooks/useLocation.js`
- Transport clients: `client/src/services/api.js`, `client/src/services/socket.js`

#### API, Authorization, and Request Pipeline
Express mounts domain routes for SOS, contacts, and users. Clerk middleware protects account-scoped operations and enforces that a user can only operate on their own saved contacts, location, SOS history, resolution, and evidence. SOS creation permits guest use while optionally associating authenticated identity. App-level middleware supplies Helmet, CORS, request limits, and rate limiting.

- Route boundary: `server/src/routes/sosRoutes.js`, `server/src/routes/contactRoutes.js`, `server/src/routes/userRoutes.js`
- Domain handlers: `server/src/controllers/sosController.js`, `server/src/controllers/contactController.js`, `server/src/controllers/userController.js`
- Security/upload middleware: `server/src/middleware/authMiddleware.js`, `server/src/middleware/uploadMiddleware.js`

#### Persistence and External Services
Mongoose models form the durable domain: user location supports nearby matching, contacts are account-owned, and SOS records retain lifecycle and evidence references. MongoDB is the system of record. Cloudinary receives multipart photo/video evidence after authenticated upload handling. Clerk supplies JWT identity on REST requests and socket handshakes.

- Data models: `server/src/models/User.js`, `server/src/models/Contact.js`, `server/src/models/SOS.js`
- Service configuration: `server/src/config/db.js`, `server/src/config/cloudinary.js`, `server/src/config/env.js`

#### Real-Time Alert Distribution
Socket.IO is attached to the server runtime. Authenticated socket connections are tracked in an in-memory socket store. On SOS creation, server logic identifies nearby connected users using saved location and broadcasts `SOS_ALERT`; it can fall back to connected recipients when no nearby stored location exists. Resolving an SOS emits `SOS_RESOLVED` to previously notified users.

- Socket/runtime ownership: `server/server.js`, `server/src/socketStore.js`
- Event-producing domain logic: `server/src/controllers/sosController.js`
- Client socket consumption: `client/src/services/socket.js`, `client/src/socket.js`

#### Key Operational Constraints
Guest SOS works without account persistence features; authenticated workflows depend on Clerk tokens. Geolocation depends on browser permission. Nearby alerts depend on both persisted location and active socket presence, so the socket store is process-local rather than durable. Environment configuration defines API/socket origins, MongoDB, Clerk, Cloudinary, rate limits, and matching radius.

- Origin/environment boundary: `server/src/config/origin.js`, `server/.env.example`, `client/.env.example`

Main entry points: [`server/src/middleware/authMiddleware.js`](https://github.com/alokranjan89/nightshield/blob/main/server/src/middleware/authMiddleware.js) (Auth & upload middleware), [`server/src/controllers/sosController.js`](https://github.com/alokranjan89/nightshield/blob/main/server/src/controllers/sosController.js) (SOS lifecycle & matching), [`client/src/context/SOSContext.jsx`](https://github.com/alokranjan89/nightshield/blob/main/client/src/context/SOSContext.jsx) (SOS state orchestration), [`server/server.js`](https://github.com/alokranjan89/nightshield/blob/main/server/server.js) (Server runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["React Client"]
  node_client_app["App shell &amp; routes<br/>React router<br/>[App.jsx]"]
  node_sos_context["SOS state orchestration<br/>React context<br/>[SOSContext.jsx]"]
  node_hold_interaction["Press-and-hold trigger<br/>React hook<br/>[useHold.js]"]
  node_location_capture["Browser location capture<br/>React hook<br/>[useLocation.js]"]
  node_emergency_ui["Emergency controls &amp; alerts<br/>React components<br/>[SOSButton.jsx]"]
  node_api_client["REST client<br/>HTTP transport<br/>[api.js]"]
  node_socket_client["Socket client<br/>Socket.IO transport<br/>[socket.js]"]
end

subgraph group_server["Node API"]
  node_server_runtime{{"Server runtime<br/>Node + Socket.IO<br/>[server.js]"}}
  node_express_app["Express request pipeline<br/>Express app<br/>[app.js]"]
  node_routes["Domain routes<br/>Express routes<br/>[sosRoutes.js]"]
  node_auth_upload["Auth &amp; upload middleware<br/>Express middleware<br/>[authMiddleware.js]"]
  node_sos_controller["SOS lifecycle &amp; matching<br/>Controller<br/>[sosController.js]"]
  node_account_controllers["Contacts &amp; user handlers<br/>Controllers"]
  node_socket_store[("Connected socket store<br/>In-memory registry<br/>[socketStore.js]")]
  node_sos_model["SOS records<br/>Mongoose model<br/>[SOS.js]"]
  node_user_model["Users &amp; saved location<br/>Mongoose model<br/>[User.js]"]
  node_contact_model["Account contacts<br/>Mongoose model<br/>[Contact.js]"]
end

subgraph group_external["External Services"]
  node_mongodb[("MongoDB<br/>System of record")]
  node_clerk{{"Clerk<br/>Identity provider"}}
  node_cloudinary[("Cloudinary<br/>Evidence storage")]
end

node_client_app -->|"drives SOS flow"| node_sos_context
node_emergency_ui -->|"press and hold"| node_hold_interaction
node_hold_interaction -->|"triggers creation"| node_sos_context
node_location_capture -->|"provides location"| node_sos_context
node_sos_context -->|"durable commands"| node_api_client
node_sos_context -->|"real-time events"| node_socket_client
node_api_client -->|"HTTPS"| node_server_runtime
node_socket_client -->|"Socket.IO"| node_server_runtime
node_server_runtime -->|"hosts"| node_express_app
node_express_app -->|"mounts"| node_routes
node_routes -->|"protects uploads and accounts"| node_auth_upload
node_auth_upload -->|"authorized SOS requests"| node_sos_controller
node_auth_upload -->|"authorized account requests"| node_account_controllers
node_auth_upload -->|"verifies JWT"| node_clerk
node_sos_controller -->|"creates and resolves"| node_sos_model
node_sos_controller -->|"nearby lookup"| node_user_model
node_account_controllers -->|"owns contacts"| node_contact_model
node_sos_model -->|"persists"| node_mongodb
node_user_model -->|"persists"| node_mongodb
node_contact_model -->|"persists"| node_mongodb
node_sos_controller -->|"finds connected recipients"| node_socket_store
node_server_runtime -->|"tracks authenticated sockets"| node_socket_store
node_sos_controller -->|"emits SOS_ALERT and SOS_RESOLVED"| node_server_runtime
node_auth_upload -->|"uploads evidence"| node_cloudinary

click node_client_app "https://github.com/alokranjan89/nightshield/blob/main/client/src/App.jsx"
click node_sos_context "https://github.com/alokranjan89/nightshield/blob/main/client/src/context/SOSContext.jsx"
click node_hold_interaction "https://github.com/alokranjan89/nightshield/blob/main/client/src/hooks/useHold.js"
click node_location_capture "https://github.com/alokranjan89/nightshield/blob/main/client/src/hooks/useLocation.js"
click node_emergency_ui "https://github.com/alokranjan89/nightshield/blob/main/client/src/components/SOSButton.jsx"
click node_api_client "https://github.com/alokranjan89/nightshield/blob/main/client/src/services/api.js"
click node_socket_client "https://github.com/alokranjan89/nightshield/blob/main/client/src/services/socket.js"
click node_server_runtime "https://github.com/alokranjan89/nightshield/blob/main/server/server.js"
click node_express_app "https://github.com/alokranjan89/nightshield/blob/main/server/src/app.js"
click node_routes "https://github.com/alokranjan89/nightshield/blob/main/server/src/routes/sosRoutes.js"
click node_auth_upload "https://github.com/alokranjan89/nightshield/blob/main/server/src/middleware/authMiddleware.js"
click node_sos_controller "https://github.com/alokranjan89/nightshield/blob/main/server/src/controllers/sosController.js"
click node_account_controllers "https://github.com/alokranjan89/nightshield/blob/main/server/src/controllers/contactController.js"
click node_socket_store "https://github.com/alokranjan89/nightshield/blob/main/server/src/socketStore.js"
click node_sos_model "https://github.com/alokranjan89/nightshield/blob/main/server/src/models/SOS.js"
click node_user_model "https://github.com/alokranjan89/nightshield/blob/main/server/src/models/User.js"
click node_contact_model "https://github.com/alokranjan89/nightshield/blob/main/server/src/models/Contact.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_sos_context,node_hold_interaction,node_location_capture,node_emergency_ui,node_api_client,node_socket_client toneBlue
class node_server_runtime,node_express_app,node_routes,node_auth_upload,node_sos_controller,node_account_controllers,node_socket_store,node_sos_model,node_user_model,node_contact_model toneAmber
class node_mongodb,node_clerk,node_cloudinary toneMint
```

## Components

### React Client

- [App shell & routes](https://github.com/alokranjan89/nightshield/blob/main/client/src/App.jsx): React router · Hosts public, dashboard, SOS, history, and settings flows. · `client/src/App.jsx`
- [SOS state orchestration](https://github.com/alokranjan89/nightshield/blob/main/client/src/context/SOSContext.jsx): React context · Coordinates SOS lifecycle and active-screen state. · `client/src/context/SOSContext.jsx`
- [Press-and-hold trigger](https://github.com/alokranjan89/nightshield/blob/main/client/src/hooks/useHold.js): React hook · `client/src/hooks/useHold.js`
- [Browser location capture](https://github.com/alokranjan89/nightshield/blob/main/client/src/hooks/useLocation.js): React hook · Uses browser permission-gated geolocation. · `client/src/hooks/useLocation.js`
- [Emergency controls & alerts](https://github.com/alokranjan89/nightshield/blob/main/client/src/components/SOSButton.jsx): React components · Provides SOS activation, incoming alerts, and status display. · `client/src/components/SOSButton.jsx`
- [REST client](https://github.com/alokranjan89/nightshield/blob/main/client/src/services/api.js): HTTP transport · Sends durable SOS, contact, user, history, resolution, and evidence commands. · `client/src/services/api.js`
- [Socket client](https://github.com/alokranjan89/nightshield/blob/main/client/src/services/socket.js): Socket.IO transport · Consumes immediate SOS alert and resolution events. · `client/src/services/socket.js`

### Node API

- [Server runtime](https://github.com/alokranjan89/nightshield/blob/main/server/server.js): Node + Socket.IO · Owns Express and the Socket.IO server. · `server/server.js`
- [Express request pipeline](https://github.com/alokranjan89/nightshield/blob/main/server/src/app.js): Express app · Applies security, CORS, limits, and mounted domain routes. · `server/src/app.js`
- [Domain routes](https://github.com/alokranjan89/nightshield/blob/main/server/src/routes/sosRoutes.js): Express routes · Exposes SOS, contacts, and user endpoints. · `server/src/routes/sosRoutes.js`
- [Auth & upload middleware](https://github.com/alokranjan89/nightshield/blob/main/server/src/middleware/authMiddleware.js): Express middleware · Verifies Clerk identity for account-scoped operations and handles evidence uploads. · `server/src/middleware/authMiddleware.js`
- [SOS lifecycle & matching](https://github.com/alokranjan89/nightshield/blob/main/server/src/controllers/sosController.js): Controller · Creates and resolves SOS records, finds recipients, and emits events. · `server/src/controllers/sosController.js`
- [Contacts & user handlers](https://github.com/alokranjan89/nightshield/blob/main/server/src/controllers/contactController.js): Controllers · Enforces account-owned contact and user operations. · `server/src/controllers/contactController.js`
- [Connected socket store](https://github.com/alokranjan89/nightshield/blob/main/server/src/socketStore.js): In-memory registry · Tracks authenticated active connections; it is process-local. · `server/src/socketStore.js`
- [SOS records](https://github.com/alokranjan89/nightshield/blob/main/server/src/models/SOS.js): Mongoose model · Persists SOS lifecycle, location, notified recipients, and evidence references. · `server/src/models/SOS.js`
- [Users & saved location](https://github.com/alokranjan89/nightshield/blob/main/server/src/models/User.js): Mongoose model · Supplies persisted identity and location for nearby matching. · `server/src/models/User.js`
- [Account contacts](https://github.com/alokranjan89/nightshield/blob/main/server/src/models/Contact.js): Mongoose model · `server/src/models/Contact.js`

### External Services

- **MongoDB**: System of record
- **Clerk**: Identity provider · Supplies JWT identity for REST and socket handshakes.
- **Cloudinary**: Evidence storage · Stores uploaded photo and video evidence.

## Connections

- App shell & routes → SOS state orchestration: drives SOS flow
- Emergency controls & alerts → Press-and-hold trigger: press and hold
- Press-and-hold trigger → SOS state orchestration: triggers creation
- Browser location capture → SOS state orchestration: provides location
- SOS state orchestration → REST client: durable commands
- SOS state orchestration → Socket client: real-time events
- REST client → Server runtime: HTTPS
- Socket client → Server runtime: Socket.IO
- Server runtime → Express request pipeline: hosts
- Express request pipeline → Domain routes: mounts
- Domain routes → Auth & upload middleware: protects uploads and accounts
- Auth & upload middleware → SOS lifecycle & matching: authorized SOS requests
- Auth & upload middleware → Contacts & user handlers: authorized account requests
- Auth & upload middleware → Clerk: verifies JWT
- SOS lifecycle & matching → SOS records: creates and resolves
- SOS lifecycle & matching → Users & saved location: nearby lookup
- Contacts & user handlers → Account contacts: owns contacts
- SOS records → MongoDB: persists
- Users & saved location → MongoDB: persists
- Account contacts → MongoDB: persists
- SOS lifecycle & matching → Connected socket store: finds connected recipients
- Server runtime → Connected socket store: tracks authenticated sockets
- SOS lifecycle & matching → Server runtime: emits SOS_ALERT and SOS_RESOLVED
- Auth & upload middleware → Cloudinary: uploads evidence
