# kelvincomputing/node-login-authentication-authorization architecture

> Architecture diagram of the GitHub repository kelvincomputing/node-login-authentication-authorization, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kelvincomputing/node-login-authentication-authorization
- Repository: https://github.com/kelvincomputing/node-login-authentication-authorization

## Overview

##### Main Components
- Client/Consumer: External clients (e.g., browsers, mobile apps, or other services) making HTTP requests.
- Presentation Layer:
- Found in the "src/presentation" folder.
- Contains subcomponents like the Express server (src/presentation/server.ts), routes (src/presentation/routes.ts), controllers (src/presentation/auth/controller.ts), and services (src/presentation/services/).
- Domain Layer:
- Located in "src/domain".
- Includes business objects and logic such as the definition of DTOs (Data Transfer Objects in src/domain/dtos), entities (e.g., user.entity.ts), and custom errors.
- Data Layer:
- Found in "src/data".
- Includes database connection and model management (in src/data/mongo and its models).
- Configuration Component:
- Found in "src/config", which includes configurations for environment variables, bcrypt, JWT, and regular expressions.
- Supporting Services:
- These incorporate the auxiliary services (like email service, authentication service) that are part of the presentation layer but provide key interactions with the domain and data layers.
- Infrastructure/DevOps:
- Docker configuration (docker-compose.yml) that suggests if the MongoDB or other services are containerized.
- Environment template (.env.template) for configuration variables.

##### Interactions Between Components
- Client sends HTTP requests (e.g., login, registration) to the Express server.
- The Express server routes these HTTP requests via specific routes (defined in src/presentation/routes.ts and auth-specific routes) to the corresponding controllers.
- Controllers in the presentation layer then interact with services (e.g., auth.service.ts, email.service.ts) that encapsulate the business logic.
- These services, where necessary, communicate with the domain layer (working with entities, DTOs, and handling business rules) for processing the request.
- Depending on the operation (e.g., saving user data), the services interact with the data layer (via the MongoDB models and connection defined in src/data/mongo) to perform CRUD operations.
- Configuration files (in src/config) provide necessary environment settings and tools (like JWT and bcrypt) that are used across these layers.
- All above interactions follow a clear separation of concerns, ensuring that each layer focuses on its responsibility.

##### Important Architectural Patterns & Principles
- Separation of Concerns: Distinct layers (configuration, presentation, domain, and data) ensure modularity.
- MVC-like or Layered Architecture: Although not a strict MVC, the separation of controllers (presentation) from services and domain logic implies an organized request handling workflow.
- Use of Adapters: The presence of adapters (e.g., jwt.adapter.ts and bcrypt.adapter.ts) indicates the utilization of adapter patterns to abstract external libraries or mechanisms.

##### Relevant Technologies & Tools
- TypeScript: All files are written in TypeScript providing type-safety.
- Express: The server framework used for building the REST API.
- MongoDB: The database used, managed with model definitions under src/data/mongo.
- JWT and Bcrypt: For authentication and security purposes.
- Docker: Containerization for database and possibly other services, as indicated by docker-compose.yml.

##### Specific Elements
- Client/Consumer (Front-end or API consumers)
- Presentation Layer (Express server, Routes, Controllers)
- Domain Layer (DTOs, Entities, Custom Errors)
- Data Layer (MongoDB, Models)
- Configuration/Adapters (JWT, bcrypt, environment configurations)
- Supporting Services (Email service, Auth service)
- From Client → Routes → Controllers → Services → Domain/Data
- Annotations where needed to show key operations like user authentication, registration, and database operations.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client/Consumer
    subgraph "Client/Consumer"
        CLIENT("External Client"):::client
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        EXPRESS("Express Server"):::presentation
        ROUTES("Routes"):::presentation
        AUTH_ROUTES("Auth Routes"):::presentation
        CONTROLLER("Auth Controller"):::presentation
        PRESENT_SERVICES("Presentation Services"):::presentation
    end

    %% Domain Layer
    subgraph "Domain Layer"
        DTO("DTOs"):::domain
        ENTITIES("Entities"):::domain
        ERRORS("Custom Errors"):::domain
    end

    %% Data Layer
    subgraph "Data Layer"
        MONGO("MongoDB"):::data
        MONGO_MODELS("Mongo Models"):::data
        DATA_INDEX("Data Layer Index"):::data
    end

    %% Configuration/Adapters
    subgraph "Configuration/Adapters"
        CONFIG("Configuration"):::config
        JWT("JWT Adapter"):::config
        BCRYPT("Bcrypt Adapter"):::config
        ENVS("Environment Settings"):::config
    end

    %% Infrastructure/DevOps
    subgraph "Infrastructure/DevOps"
        DOCKER("Docker Compose"):::infra
        ENV_TEMPLATE("Environment Template"):::infra
    end

    %% Data Flow
    CLIENT -->|"HTTP Requests"| EXPRESS
    EXPRESS -->|"Routes"| ROUTES
    EXPRESS -->|"Auth Routes"| AUTH_ROUTES
    ROUTES -->|"Calls"| CONTROLLER
    AUTH_ROUTES -->|"Calls"| CONTROLLER
    CONTROLLER -->|"Uses"| PRESENT_SERVICES
    PRESENT_SERVICES -->|"ExecutesBusinessLogic"| DTO
    PRESENT_SERVICES -->|"ExecutesBusinessLogic"| ENTITIES
    PRESENT_SERVICES -->|"ValidatesUsing"| ERRORS
    PRESENT_SERVICES -->|"PerformsCRUD"| MONGO_MODELS
    MONGO_MODELS -->|"DBOperations"| MONGO
    PRESENT_SERVICES -->|"Accesses"| DATA_INDEX
    EXPRESS -->|"Loads"| CONFIG
    CONFIG -->|"Includes"| JWT
    CONFIG -->|"Includes"| BCRYPT
    CONFIG -->|"Includes"| ENVS
    DOCKER -->|"Contains"| MONGO

    %% Click Events for Presentation Layer
    click EXPRESS "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/presentation/server.ts"
    click ROUTES "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/presentation/routes.ts"
    click AUTH_ROUTES "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/presentation/auth/routes.ts"
    click CONTROLLER "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/presentation/auth/controller.ts"
    click PRESENT_SERVICES "https://github.com/kelvincomputing/node-login-authentication-authorization/tree/main/src/presentation/services/"

    %% Click Events for Domain Layer
    click DTO "https://github.com/kelvincomputing/node-login-authentication-authorization/tree/main/src/domain/dtos/auth/"
    click ENTITIES "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/domain/entities/user.entity.ts"
    click ERRORS "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/domain/errors/custom.error.ts"

    %% Click Events for Data Layer
    click MONGO_MODELS "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/data/mongo/models/user.model.ts"
    click MONGO "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/data/mongo/mongo-database.ts"
    click DATA_INDEX "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/data/index.ts"

    %% Click Events for Configuration/Adapters
    click CONFIG "https://github.com/kelvincomputing/node-login-authentication-authorization/tree/main/src/config/"
    click JWT "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/config/jwt.adapter.ts"
    click BCRYPT "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/config/bcrypt.adapter.ts"
    click ENVS "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/src/config/envs.ts"

    %% Click Events for Infrastructure/DevOps
    click DOCKER "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/docker-compose.yml"
    click ENV_TEMPLATE "https://github.com/kelvincomputing/node-login-authentication-authorization/blob/main/.env.template"

    %% Styles
    classDef client fill:#F9E79F,stroke:#7D6608,stroke-width:2px;
    classDef presentation fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef domain fill:#A9DFBF,stroke:#1D8348,stroke-width:2px;
    classDef data fill:#FAD7A0,stroke:#D35400,stroke-width:2px;
    classDef config fill:#F5B7B1,stroke:#922B21,stroke-width:2px;
    classDef infra fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
```
