# nikitapryymak/mern-auth-jwt architecture

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

- Interactive diagram: https://gitdiagram.com/nikitapryymak/mern-auth-jwt
- Repository: https://github.com/nikitapryymak/mern-auth-jwt

## Overview

##### Project type and purpose
- This is a full-stack MERN (MongoDB, Express, React, Node) authentication template using JWT.
- It provides user registration, login/logout, email verification, password reset and session management.
- Frontend: React + Chakra UI + React Query; Backend: TypeScript + Express; Database: MongoDB; Email service: Resend.

##### File structure
- Top-level directories:
– backend/ – all API logic organized in layers: routes → controllers → services → models
– frontend/ – React single-page app with components, pages, hooks, config, theme
– caddy/ – Caddy reverse-proxy configurations & Dockerfiles
– docker-compose.yml & docker-compose.dev.yml – orchestration for local/dev containers
- Config & env samples in both frontend and backend.
- Middleware (authentication, error handling), utility modules (jwt, bcrypt, email templates) in backend.
- React Query and custom hooks handle data fetching and caching on the client.

##### README for additional insights
- It describes a layered API architecture (routes → controllers → services → models).
- It shows the JWT authentication flow (short-lived access token + long-lived refresh token in HTTP-only cookies).
- Mentions Postman collection for testing.

##### Elements
– Clear labels for each component (e.g., “React SPA,” “Express API – Controller Layer”).
- Backend layers in another (e.g., green)
- External services/data stores in a third (e.g., orange)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Zone
    subgraph "Frontend (React SPA)" 
        direction TB
        SPA["React SPA"]:::frontend
        Components["Components"]:::frontend
        Pages["Pages"]:::frontend
        Theme["Chakra UI Theme"]:::frontend
        Hooks["Custom Hooks"]:::frontend
        APIClient["API Client"]:::frontend
        QueryClient["Query Client"]:::frontend
        SPA -->|uses| Components
        SPA -->|navigates| Pages
        SPA -->|applies| Theme
        SPA -->|calls API| APIClient
        SPA -->|caches data| QueryClient
        SPA -->|invokes| Hooks
    end

    %% Proxy
    Caddy["Caddy Reverse Proxy"]:::infrastructure

    %% Backend Zone
    subgraph "Backend (Express API)" 
        direction TB
        Entry["API Entry Point"]:::backend
        MiddlewareAuth["Authenticate Middleware"]:::backend
        MiddlewareErr["Error Handler Middleware"]:::backend
        subgraph "Routes Layer"
            direction TB
            AuthRoutes["Auth Routes"]:::backend
            UserRoutes["User Routes"]:::backend
            SessionRoutes["Session Routes"]:::backend
        end
        subgraph "Controllers Layer"
            direction TB
            AuthCtrl["Auth Controller"]:::backend
            UserCtrl["User Controller"]:::backend
            SessionCtrl["Session Controller"]:::backend
        end
        subgraph "Services Layer"
            direction TB
            AuthService["Auth Service"]:::backend
        end
        subgraph "Models Layer"
            direction TB
            UserModel["User Model"]:::backend
            SessionModel["Session Model"]:::backend
            VerifModel["VerificationCode Model"]:::backend
        end
        subgraph "Utilities & Config"
            direction TB
            JWTUtil["JWT Utils"]:::backend
            CookieUtil["Cookie Helpers"]:::backend
            BcryptUtil["Bcrypt Wrapper"]:::backend
            MailUtil["Email Sender"]:::backend
            Templates["Email Templates"]:::backend
            DBConfig["DB Config"]:::backend
            ResendConfig["Resend Config"]:::backend
        end

        Entry -->|use| MiddlewareAuth
        Entry -->|use| MiddlewareErr
        Entry -->|register routes| AuthRoutes
        Entry -->|register routes| UserRoutes
        Entry -->|register routes| SessionRoutes
        AuthRoutes -->|call| AuthCtrl
        UserRoutes -->|call| UserCtrl
        SessionRoutes -->|call| SessionCtrl
        AuthCtrl -->|invoke| AuthService
        SessionCtrl -->|invoke| AuthService
        UserCtrl -->|invoke| AuthService
        AuthService -->|read/write| UserModel
        AuthService -->|read/write| SessionModel
        AuthService -->|read/write| VerifModel
        AuthService -->|use| JWTUtil
        AuthService -->|use| CookieUtil
        AuthService -->|use| BcryptUtil
        AuthService -->|use| MailUtil
        MailUtil -->|use templates| Templates
        Entry -->|connect| DBConfig
        Entry -->|init| ResendConfig
    end

    %% Data Stores & External
    MongoDB["MongoDB Database"]:::external
    Resend["Resend Email Service"]:::external
    DockerCompose["Docker Compose"]:::infrastructure

    %% Flows
    SPA -->|HTTP requests| Caddy
    Caddy -->|proxy| Entry
    AuthService -->|store sessions| MongoDB
    AuthService -->|store users| MongoDB
    AuthService -->|send emails| Resend
    DockerCompose -->|orchestrates| Caddy
    DockerCompose -->|orchestrates| Entry
    DockerCompose -->|orchestrates| MongoDB
    DockerCompose -->|orchestrates| SPA

    %% Click Events - Frontend
    click SPA "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/frontend/src"
    click Components "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/frontend/src/components"
    click Pages "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/frontend/src/pages"
    click Theme "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/frontend/src/theme"
    click Hooks "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/frontend/src/hooks"
    click APIClient "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/frontend/src/config/apiClient.js"
    click QueryClient "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/frontend/src/config/queryClient.js"
    click SPA "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/frontend/Dockerfile"
    click Caddy "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/frontend/Caddyfile"

    %% Click Events - Caddy
    click Caddy "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/caddy/Caddyfile"
    click Caddy "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/caddy/Caddyfile.dev"
    click Caddy "https://github.com/nikitapryymak/mern-auth-jwt/tree/main/caddy/Dockerfile"

    %% Click Events - Docker Compose
    click DockerCompose "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/docker-compose.yml"
    click DockerCompose "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/docker-compose.dev.yml"

    %% Click Events - Backend
    click Entry "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/index.ts"
    click AuthRoutes "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/routes/auth.route.ts"
    click UserRoutes "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/routes/user.route.ts"
    click SessionRoutes "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/routes/session.route.ts"
    click AuthCtrl "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/controllers/auth.controller.ts"
    click UserCtrl "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/controllers/user.controller.ts"
    click SessionCtrl "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/controllers/session.controller.ts"
    click AuthService "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/services/auth.service.ts"
    click UserModel "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/models/user.model.ts"
    click SessionModel "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/models/session.model.ts"
    click VerifModel "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/models/verificationCode.model.ts"
    click MiddlewareAuth "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/middleware/authenticate.ts"
    click MiddlewareErr "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/middleware/errorHandler.ts"
    click JWTUtil "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/utils/jwt.ts"
    click CookieUtil "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/utils/cookies.ts"
    click BcryptUtil "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/utils/bcrypt.ts"
    click MailUtil "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/utils/sendMail.ts"
    click Templates "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/utils/emailTemplates.ts"
    click DBConfig "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/config/db.ts"
    click ResendConfig "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/src/config/resend.ts"
    click DockerCompose "https://github.com/nikitapryymak/mern-auth-jwt/blob/main/backend/postman.json"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#005B96,stroke-width:2px;
    classDef backend fill:#E0F7EA,stroke:#2E7D32,stroke-width:2px;
    classDef external fill:#FFE5B4,stroke:#E65100,stroke-width:2px;
    classDef infrastructure fill:#F0F0F0,stroke:#757575,stroke-width:2px;
```
