# harshit977/user-authentication-system-with-nodejs architecture

> Architecture diagram of the GitHub repository harshit977/user-authentication-system-with-nodejs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/harshit977/user-authentication-system-with-nodejs
- Repository: https://github.com/harshit977/user-authentication-system-with-nodejs

## Overview

##### Project type and purpose
- This is a standalone backend service (no separate frontend) providing user authentication APIs (signup, login, logout)
- It is built on Node.js/Express and uses MongoDB for persistence
- All token handling is home-grown: custom expirable tokens encrypted with the Node.js crypto module

##### File structure
- app.js: Express application entry point, wires up routes and middleware
- authenticate.js: authentication middleware that decrypts & validates tokens on protected endpoints
- encryption.js: module responsible for creating and decrypting expirable tokens
- routes/: Express routers
- user.js → public endpoints for register/login/logout
- protected.js → endpoints that require a valid token
- controllers/: business-logic handlers
- user.js → signup/login/logout workflows
- loggedin.js → logic for protected routes once authentication passes
- models/user.js: Mongoose schema & data-access for User collection

##### README for additional insights
- Custom tokens (not JWT) that embed an expiry, encrypted with a secret key
- No external auth libraries, everything built from scratch
- Uses ExpressJS middleware pattern, Mongoose for MongoDB

##### Detailed component breakdown
- “HTTP POST /user/signup” and “POST /user/login”
- “GET /protected/...” with Authorization header
1. ROUTES layer: /user, /protected
2. AUTH MIDDLEWARE: authenticate.js
3. CONTROLLERS: user.js, loggedin.js
4. ENCRYPTION SERVICE: encryption.js
5. MODELS (Mongoose): models/user.js
- User collection documents
- Client → Routes
- Routes → Controllers
- Controllers → Encryption Service (for token)
- Controllers → Models → DB
- Middleware → Encryption Service (token validation) → Controllers
- OPTIONAL: annotate token format (encrypted payload + expiry)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    Clients["Clients (Web/Mobile)"]:::clients
    click Clients "https://github.com/harshit977/user-authentication-system-with-nodejs/tree/master/N/A"

    %% Express App
    ExpressApp["Express App"]:::server
    click ExpressApp "https://github.com/harshit977/user-authentication-system-with-nodejs/blob/master/app.js"

    %% Routes Layer
    subgraph "Routes Layer"
        UserRoutes["/user (Routes)"]:::routes
        click UserRoutes "https://github.com/harshit977/user-authentication-system-with-nodejs/blob/master/routes/user.js"
        ProtectedRoutes["/protected (Routes)"]:::routes
        click ProtectedRoutes "https://github.com/harshit977/user-authentication-system-with-nodejs/blob/master/routes/protected.js"
    end

    %% Middleware
    AuthMiddleware["Authentication Middleware"]:::middleware
    click AuthMiddleware "https://github.com/harshit977/user-authentication-system-with-nodejs/blob/master/authenticate.js"

    %% Controllers
    subgraph "Controllers"
        UserController["User Controller"]:::controller
        click UserController "https://github.com/harshit977/user-authentication-system-with-nodejs/blob/master/controllers/user.js"
        LoggedinController["Protected Controller"]:::controller
        click LoggedinController "https://github.com/harshit977/user-authentication-system-with-nodejs/blob/master/controllers/loggedin.js"
    end

    %% Services
    EncryptionService["Encryption Service"]:::service
    click EncryptionService "https://github.com/harshit977/user-authentication-system-with-nodejs/blob/master/encryption.js"

    %% Models & Database
    UserModel["User Model"]:::model
    click UserModel "https://github.com/harshit977/user-authentication-system-with-nodejs/blob/master/models/user.js"
    MongoDB["MongoDB"]:::db

    %% Flow
    Clients -->|HTTP Request| ExpressApp
    ExpressApp -->|handles routes| UserRoutes
    ExpressApp -->|handles routes| ProtectedRoutes

    UserRoutes -->|invokes| UserController
    UserController -->|read/write| UserModel
    UserModel -->|persist| MongoDB
    UserController -->|issue token| EncryptionService
    EncryptionService -->|token issued| UserController
    UserController -->|response with token| Clients

    ProtectedRoutes -->|passes through| AuthMiddleware
    AuthMiddleware -->|decrypt & validate| EncryptionService
    EncryptionService -->|return user info| AuthMiddleware
    AuthMiddleware -->|forward| LoggedinController
    LoggedinController -->|business logic| UserModel
    LoggedinController -->|response| Clients

    %% Styles
    classDef clients fill:#FFEECC,stroke:#E69A00,stroke-width:2px
    classDef server fill:#CCEEFF,stroke:#0094D4,stroke-width:2px
    classDef routes fill:#E0F7FA,stroke:#006064,stroke-width:2px
    classDef middleware fill:#FFF3E0,stroke:#E65100,stroke-width:2px
    classDef controller fill:#E8F5E9,stroke:#2E7D32,stroke-width:2px
    classDef service fill:#F3E5F5,stroke:#6A1B9A,stroke-width:2px
    classDef model fill:#FFEBEE,stroke:#C62828,stroke-width:2px
    classDef db fill:#E3F2FD,stroke:#1565C0,stroke-width:2px
```
