# auth-wave/authwave-backend architecture

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

- Interactive diagram: https://gitdiagram.com/auth-wave/authwave-backend
- Repository: https://github.com/auth-wave/authwave-backend

## Overview

##### Project Type and Purpose
- Auth Wave is a Node.js backend service built with Express and TypeScript that provides robust authentication and user management features.
- The service supports multiple authentication methods (email/password, magic URL, OTP), session tracking, user and project management, and security logging.
- It is designed as a modular backend API server that interacts with MongoDB and includes a separate admin dashboard for managing projects and monitoring security events.

##### File Structure
- The top-level structure clearly separates the core application entry points (src/app.ts, src/index.ts), controllers (business logic for different resource types), middlewares (e.g., for authentication and rate limiting), models (data schema definitions), routes (endpoint definitions), features (authentication modules like account lockout, email, otp, etc.), and utilities.
- The dedicated directories for “controllers,” “models,” “routes,” and “middlewares” indicate a separation of concerns—a common architectural pattern in Node.js/Express applications.
- The “database” directory suggests isolated configuration for MongoDB.
- There are also “schema” and “types” directories that reinforce strong typing and validation using TypeScript and tools like Zod.

##### README for Additional Insights
- The README outlines core features including multiple authentication methods, detailed session management, and a robust security logging mechanism.
- It highlights the separation between user-facing endpoints and the admin dashboard.
- Security measures like JWT token management, rate limiting, and project-level configuration are mentioned, indicating that there are clear paths for data flow, error handling, and security controls.
- The technical stack shows that while the backend is built with Node.js/Express and TypeScript with MongoDB as the database, there is also a separate frontend for the admin dashboard built with Next.js.

a. Main Components:
- Client Applications & Admin Dashboard: These interact with the backend API.
- API Gateway/Server: Represent the Express server; include modules for request handling (src/index.ts & src/app.ts).
- Routes & Controllers: Separate endpoints for user, admin, project, and security-log management.
- Middlewares: Handle authentication (like user-auth.ts, admin-auth.ts), rate limitation (api-limit.ts), and validation.
- Database: MongoDB (used via Mongoose) for persistence of user data, projects, sessions, and security logs.
- Utility & Feature Modules: Include components such as token generator, account lockout, email service, and OTP functionality.
- Error Handling and Logging: Custom utilities for error management and logging (e.g., async-handler, logger).

b. Relationships and Interactions:
- Incoming client requests go from the Client Admin Dashboard or application frontends into the API server.
- Requests are routed first through middlewares (auth, validation, API limits) then to specific route handlers.
- Controllers handle business logic, interfacing with models and feature modules to process logic like authentication, registration, session management, project configuration, etc.
- The controllers then perform data operations via the database layer.
- Responses (including errors) are created using custom response and error utilities.

c. Architectural Patterns and Design Principles:
- Modularity and Separation of Concerns: Clear boundaries among routes, controllers, models, middlewares and utilities.
- Layered Architecture: Presentation (API endpoints), business logic (controllers/features), data access (database modules/models).
- Security by Design: Dedicated token management, session security, rate limiting handled at middleware level.
- Scalable & Maintainable Code: Usage of TypeScript for type safety and explicit schemas/validations.

d. Relevant Technologies and Frameworks:
- Backend Framework: Express.js with Node.js and TypeScript.
- Database: MongoDB with Mongoose.
- Libraries: bcrypt (for password hashing), JWT (for token management), Zod (for validation).
- Frontend technologies are mentioned in the README (Next.js, Tailwind CSS, Shadcn UI) for the administered portion of the project.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Clients Cluster
    subgraph "Clients"
        CA["Client Applications"]:::client
        AD["Admin Dashboard"]:::client
    end

    %% API Server (Express)
    API_Server["API Server (Express)"]:::api

    %% Middlewares Cluster
    subgraph "Middlewares"
        A_Auth["Admin Authentication"]:::middleware
        U_Auth["User Authentication"]:::middleware
        API_Limit["API Rate Limiting"]:::middleware
        Project_Valid["Project Validation"]:::middleware
    end

    %% Routes Cluster
    subgraph "Routes"
        AR["Admin Routes"]:::route
        PR["Project Routes"]:::route
        SLR["Security Log Routes"]:::route
        UR["User Routes"]:::route
    end

    %% Controllers Cluster
    subgraph "Controllers"
        AC["Admin Controller"]:::controller
        PC["Project Controller"]:::controller
        SLC["Security Log Controller"]:::controller
        UC["User Controller"]:::controller
    end

    %% Database & Models Cluster
    subgraph "Database & Models"
        DBConf["Database Config"]:::database
        DataMod["Data Models"]:::database
    end

    %% Feature Modules Cluster
    subgraph "Feature Modules"
        AL["Account Lockout"]:::feature
        ES["Email Service"]:::feature
        OTP["OTP Functionality"]:::feature
        PL["Project Limit"]:::feature
        SL["Security Logging"]:::feature
    end

    %% Utilities & Helpers Cluster
    subgraph "Utilities & Helpers"
        UTIL["Utilities"]:::utility
    end

    %% Schema & Type Validation Cluster
    subgraph "Schema & Type Validation"
        SCH["Schemas"]:::schema
        TYP["Types"]:::schema
    end

    %% Connections between layers
    CA --> API_Server
    AD --> API_Server
    API_Server --> A_Auth

    %% Middlewares internal flow
    A_Auth --> U_Auth
    U_Auth --> API_Limit
    API_Limit --> Project_Valid

    %% From Middlewares to Routes
    Project_Valid --> AR

    %% Routes internal flow
    AR --> PR
    PR --> SLR
    SLR --> UR

    %% From Routes to Controllers
    UR --> AC

    %% Controllers internal flow
    AC --> PC
    PC --> SLC
    SLC --> UC

    %% From Controllers to Database & Models
    UC --> DBConf
    DBConf --> DataMod

    %% From Controllers to Feature Modules
    UC --> AL
    AL --> ES
    ES --> OTP
    OTP --> PL
    PL --> SL

    %% From Controllers to Utilities & Helpers and Schema
    UC --> UTIL
    UC --> SCH
    SCH --> TYP

    %% Click Events for API Server
    click API_Server "https://github.com/auth-wave/authwave-backend/blob/main/src/app.ts"
    click API_Server "https://github.com/auth-wave/authwave-backend/blob/main/src/index.ts"

    %% Click Events for Controllers
    click AC "https://github.com/auth-wave/authwave-backend/blob/main/src/controllers/admin.controller.ts"
    click PC "https://github.com/auth-wave/authwave-backend/blob/main/src/controllers/project.controller.ts"
    click SLC "https://github.com/auth-wave/authwave-backend/blob/main/src/controllers/security-log.controller.ts"
    click UC "https://github.com/auth-wave/authwave-backend/blob/main/src/controllers/user.controller.ts"

    %% Click Events for Routes
    click AR "https://github.com/auth-wave/authwave-backend/blob/main/src/routes/admin.route.ts"
    click PR "https://github.com/auth-wave/authwave-backend/blob/main/src/routes/project.route.ts"
    click SLR "https://github.com/auth-wave/authwave-backend/blob/main/src/routes/security-log.route.ts"
    click UR "https://github.com/auth-wave/authwave-backend/blob/main/src/routes/user.route.ts"

    %% Click Events for Middlewares
    click A_Auth "https://github.com/auth-wave/authwave-backend/blob/main/src/middlewares/admin-auth.ts"
    click U_Auth "https://github.com/auth-wave/authwave-backend/blob/main/src/middlewares/user-auth.ts"
    click API_Limit "https://github.com/auth-wave/authwave-backend/blob/main/src/middlewares/api-limit.ts"
    click Project_Valid "https://github.com/auth-wave/authwave-backend/blob/main/src/middlewares/validate-project.ts"

    %% Click Events for Database & Models
    click DBConf "https://github.com/auth-wave/authwave-backend/blob/main/src/database/db-config.ts"
    click DataMod "https://github.com/auth-wave/authwave-backend/tree/main/src/models"

    %% Click Events for Feature Modules
    click AL "https://github.com/auth-wave/authwave-backend/blob/main/src/features/account-lockout.ts"
    click ES "https://github.com/auth-wave/authwave-backend/blob/main/src/features/email.ts"
    click OTP "https://github.com/auth-wave/authwave-backend/blob/main/src/features/otp.ts"
    click PL "https://github.com/auth-wave/authwave-backend/blob/main/src/features/project-limit.ts"
    click SL "https://github.com/auth-wave/authwave-backend/blob/main/src/features/security-log.ts"

    %% Click Event for Utilities & Helpers
    click UTIL "https://github.com/auth-wave/authwave-backend/tree/main/src/utils"

    %% Click Events for Schema & Type Validation
    click SCH "https://github.com/auth-wave/authwave-backend/tree/main/src/schema"
    click TYP "https://github.com/auth-wave/authwave-backend/tree/main/src/types"

    %% Styles
    classDef client fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef api fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef middleware fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef route fill:#fde2e2,stroke:#c92a2a,stroke-width:2px;
    classDef controller fill:#e2e3e5,stroke:#41464b,stroke-width:2px;
    classDef database fill:#d1ecf1,stroke:#0c5460,stroke-width:2px,stroke-dasharray: 5,5;
    classDef feature fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef utility fill:#fff,stroke:#6c757d,stroke-width:2px;
    classDef schema fill:#e2eaff,stroke:#004085,stroke-width:2px;
```
