# the-marcy-lab-school/react-express-auth architecture

> Architecture diagram of the GitHub repository the-marcy-lab-school/react-express-auth, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/the-marcy-lab-school/react-express-auth
- Repository: https://github.com/the-marcy-lab-school/react-express-auth

## Overview

##### Project Type and Purpose
- This is a full-stack web application that provides user authentication and profile management.
- Frontend: React SPA (Vite)
- Backend: Node.js + Express with cookie-based sessions and a PostgreSQL database accessed via Knex.js

##### Main Architectural Components
a. Client / Browser
- Runs the React single‐page application (SPA).
- Hosts UI components, pages, contexts, and adapters for communicating with the backend.
b. Frontend Hosting (Static Assets)
- Built by `npm run build` (Vite) into a static bundle (HTML/JS/CSS).
- Can be served via a CDN, nginx, or the Express server itself.
c. Backend API Server (Express)
- Exposes RESTful endpoints under `/auth` and `/users`.
- Middleware stack: request logging → cookie session handling → authentication check → route handlers → error logging/handling.
- Controllers (authControllers.js, userControllers.js) implement business logic.
d. Database Layer
- PostgreSQL database defined by `.env` configuration.
- Knex.js for migrations, seeds, and query building.
- Data model: `User` in server/models/User.js
e. External & Dev Tools
- Postgres (external service)
- Knex migrations & seeds
- Environment variables (.env)

##### Relationships & Data Flow
1. User opens browser → React app is loaded (index.html + bundle).
2. React components → call fetch() utility in src/utils → adapters (auth-adapter, user-adapter) → HTTP requests to Express API.
3. Express middleware parses cookies, establishes `req.session.userId`, and logs the request.
4. Controllers call model methods → Knex queries → Postgres database.
5. Responses (JSON) flow back through middleware → to React adapters → update context & components.
6. Session cookie is set/updated in browser on login/logout.

##### Architectural Patterns & Design Principles
- Separation of Concerns:
- Frontend: UI / state management / API adapters
- Backend: HTTP layer / middleware / controllers / models
- Layered Architecture on Server:
1. Middleware layer (authentication, sessions, logging, error handling)
2. Controller layer (HTTP → business logic)
3. Data access layer (Knex + Models → DB)
- Component-Based UI on Frontend:
- Pages, presentational components, context providers, utility modules

##### Final Checklist
- Grouping or layering to show middleware → controllers → models on the server
- Annotation of key technologies: React, Vite, Express, Knex, PostgreSQL

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client / Browser Layer
    subgraph "Client / Browser"
        direction TB
        ReactSPA["React SPA"]:::frontend
        UIComponents["UI Components"]:::frontend
        PageComponents["Page Components"]:::frontend
        ContextProvider["CurrentUserContextProvider"]:::frontend
        ContextDefinition["current-user-context.js"]:::frontend
        AuthAdapter["auth-adapter.js"]:::frontend
        UserAdapter["user-adapter.js"]:::frontend
        FetchUtils["fetchingUtils.js"]:::frontend
        PublicFolder["Public (Static Assets)"]:::external
        IndexHTML["index.html"]:::public
        ViteConfig["vite.config.js"]:::config
    end

    %% Static Asset Server
    StaticServer["Static Asset Server\n(CDN/nginx/Express)"]:::external

    %% Backend API Server
    subgraph "Backend API (Express)"
        direction TB
        subgraph "Middleware Layer"
            LogRoutes["Route Logging"]:::backend
            HandleCookie["Cookie Session Handling"]:::backend
            AuthCheck["Authentication Check"]:::backend
            LogErrors["Error Logging/Handling"]:::backend
        end
        subgraph "Controller Layer"
            AuthControllers["authControllers.js"]:::backend
            UserControllers["userControllers.js"]:::backend
        end
        subgraph "Data Access Layer"
            KnexInstance["Knex.js Instance"]:::backend
            UserModel["User Model"]:::backend
        end
        EnvTemplate[".env.template"]:::config
    end

    %% Database and Tools
    DB["PostgreSQL Database"]:::db
    MigrationsSeeds["Knex Migrations & Seeds"]:::db_tools

    %% Data Flows
    ReactSPA -->|"GET index.html, bundle.js\nHTTP"| StaticServer
    StaticServer -->|"Serves Assets"| ReactSPA

    ReactSPA -->|"XHR/REST (JSON)\nHTTP + session cookie"| LogRoutes
    LogRoutes --> HandleCookie --> AuthCheck -->|"to controllers"| AuthControllers
    AuthCheck --> UserControllers
    AuthControllers --> UserModel
    UserControllers --> UserModel
    UserModel --> KnexInstance -->|"SQL queries"| DB

    MigrationsSeeds --> DB

    %% Click Events
    click ReactSPA "https://github.com/the-marcy-lab-school/react-express-auth/tree/main/frontend/src"
    click UIComponents "https://github.com/the-marcy-lab-school/react-express-auth/tree/main/frontend/src/components"
    click PageComponents "https://github.com/the-marcy-lab-school/react-express-auth/tree/main/frontend/src/pages"
    click ContextProvider "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/frontend/src/contexts/CurrentUserContextProvider.jsx"
    click ContextDefinition "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/frontend/src/contexts/current-user-context.js"
    click AuthAdapter "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/frontend/src/adapters/auth-adapter.js"
    click UserAdapter "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/frontend/src/adapters/user-adapter.js"
    click FetchUtils "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/frontend/src/utils/fetchingUtils.js"
    click PublicFolder "https://github.com/the-marcy-lab-school/react-express-auth/tree/main/frontend/public"
    click IndexHTML "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/frontend/index.html"
    click ViteConfig "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/frontend/vite.config.js"
    click LogRoutes "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/middleware/logRoutes.js"
    click HandleCookie "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/middleware/handleCookieSessions.js"
    click AuthCheck "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/middleware/checkAuthentication.js"
    click LogErrors "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/middleware/logErrors.js"
    click AuthControllers "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/controllers/authControllers.js"
    click UserControllers "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/controllers/userControllers.js"
    click KnexInstance "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/db/knex.js"
    click EnvTemplate "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/.env.template"
    click MigrationsSeeds "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/db/migrations/20230423034706_create_users_table.js"
    click MigrationsSeeds "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/db/migrations/20240514201137_add_timestamps_to_users.js"
    click MigrationsSeeds "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/db/seeds/init.js"
    click UserModel "https://github.com/the-marcy-lab-school/react-express-auth/blob/main/server/models/User.js"

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef backend fill:#90EE90,stroke:#333,stroke-width:1px
    classDef db fill:#FFA500,stroke:#333,stroke-width:1px
    classDef external fill:#cccccc,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef db_tools fill:#FFD580,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
    classDef config fill:#D3D3D3,stroke:#333,stroke-width:1px,stroke-dasharray: 2 2
```
