# bensonmacharia/jwt-go-rbac architecture

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

- Interactive diagram: https://gitdiagram.com/bensonmacharia/jwt-go-rbac
- Repository: https://github.com/bensonmacharia/jwt-go-rbac

## Overview

##### Main Components

A. Authentication Layer
- JWT Token Management (from util/jwt.go and util/jwtAuth.go)
- Login/Register Handlers
- Token Validation & Authorization Middleware

B. API Controllers Layer
- User Controller
- Role Controller
- Room Controller
- Booking Controller

C. Model Layer
- User Model
- Role Model
- Room Model
- Booking Model
- Authentication Models (Login/Register)

D. Database Layer
- Database Connection & Management

- [Client/External Requests]
- ↓
- [Authentication Layer]
- ↓
- [API Controllers Layer]
- ↓
- [Model Layer]
- ↓
- [Database Layer]

##### Specific Component Relationships to Highlight

- How JWT authentication flow works:
- Client → Login/Register → JWT Generation → Token Storage
- Demonstrate RBAC flow:
- Request → JWT Auth Middleware → Role Verification → Controller Access
- Database interactions:
- Controller → Model → Database

##### Implementation Details

- JWT Token Flow:
- Token Generation
- Token Validation
- Role Extraction
- Authorization Checks

- API Endpoints Groups:
- Authentication endpoints
- User management endpoints
- Room management endpoints
- Booking management endpoints

##### Technical Stack Indicators

- Go (Golang)
- JWT-Go package
- Database system (from database.go)
- HTTP routing system

Remember to:
- Keep the authentication flow clear and prominent
- Role-based access control checkpoints
- Clear separation between public and protected resources
- Where and how JWT tokens are validated
- Demonstrate the relationship between roles and accessible resources

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        C[Client/External Requests]
    end

    subgraph Auth
        JWT["JWT Token Management"]:::auth
        Login["Login/Register Handler"]:::auth
        AuthMW["JWT Auth Middleware"]:::auth
    end

    subgraph Controllers
        UC["User Controller"]:::controller
        RC["Role Controller"]:::controller
        RoomC["Room Controller"]:::controller
        BC["Booking Controller"]:::controller
    end

    subgraph Models
        UM["User Model"]:::model
        RoleM["Role Model"]:::model
        RoomM["Room Model"]:::model
        BM["Booking Model"]:::model
        AuthM["Auth Models"]:::model
    end

    subgraph Database
        DB[(Database)]:::db
    end

    C --> Login
    C --> AuthMW
    Login --> JWT
    JWT --> AuthMW
    AuthMW --> UC
    AuthMW --> RC
    AuthMW --> RoomC
    AuthMW --> BC

    UC --> UM
    RC --> RoleM
    RoomC --> RoomM
    BC --> BM
    Login --> AuthM

    UM --> DB
    RoleM --> DB
    RoomM --> DB
    BM --> DB
    AuthM --> DB

    %% Click Events
    click JWT "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/util/jwt.go"
    click AuthMW "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/util/jwtAuth.go"
    click UC "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/controller/user.go"
    click RC "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/controller/role.go"
    click RoomC "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/controller/room.go"
    click BC "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/controller/booking.go"
    click UM "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/model/user.go"
    click RoleM "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/model/role.go"
    click RoomM "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/model/room.go"
    click BM "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/model/booking.go"
    click AuthM "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/model/login.go"
    click DB "https://github.com/bensonmacharia/jwt-go-rbac/blob/main/database/database.go"

    %% Styles
    classDef auth fill:#90EE90,stroke:#333,stroke-width:2px
    classDef controller fill:#ADD8E6,stroke:#333,stroke-width:2px
    classDef model fill:#FFE4B5,stroke:#333,stroke-width:2px
    classDef db fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef default fill:#fff,stroke:#333,stroke-width:2px

    %% Legend
    subgraph Legend
        L1[Authentication]:::auth
        L2[Controllers]:::controller
        L3[Models]:::model
        L4[(Database)]:::db
    end
```
