Module Xác thực

Mô tả

Module Xác thực quản lý việc xác minh danh tính người dùng, đăng ký, đăng nhập/đăng xuất và quản lý mật khẩu cho cả người dùng admin và người dùng ứng dụng chung. Module này cung cấp quyền truy cập an toàn vào ứng dụng sử dụng Firebase để xác thực, hỗ trợ cả phương thức xác thực email/mật khẩu và tài khoản Google.

Sơ đồ hoạt động tổng quan

---
config:
  theme: base
  layout: dagre
  flowchart:
    curve: linear
    htmlLabels: true
  themeVariables:
    edgeLabelBackground: "transparent"
---
flowchart TB
    %% Main components
    Client[Client Application]
    AuthController[AuthController]
    AuthService(AuthService)
    Firebase((Firebase Auth))
    UserDB[(users)]
    RoleDB[(roles)]
    SessionDB[(sessions)]

    Client --- Step1[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>1</span>
            <p style='margin-top: 8px'>Gửi yêu cầu xác thực</p>
        </div>
    ]
    Step1 --> AuthController

    AuthController --- Step2[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>2</span>
            <p style='margin-top: 8px'>Xác thực yêu cầu</p>
        </div>
    ]
    Step2 --> AuthService

    AuthService --- Step3[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>3</span>
            <p style='margin-top: 8px'>Xác thực với Firebase</p>
        </div>
    ]
    Step3 --> Firebase

    Firebase --- Step4[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>4</span>
            <p style='margin-top: 8px'>Trả về Token/Thông tin user</p>
        </div>
    ]
    Step4 --> AuthService

    AuthService --- Step5[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>5</span>
            <p style='margin-top: 8px'>Tìm User</p>
        </div>
    ]
    Step5 --> UserDB

    AuthService --- Step5_1[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>5.1</span>
            <p style='margin-top: 8px'>Kiểm tra vai trò User</p>
        </div>
    ]
    Step5_1 --> RoleDB

    AuthService --- Step6[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>6</span>
            <p style='margin-top: 8px'>Tạo Session</p>
        </div>
    ]
    Step6 --> SessionDB

    AuthService --- Step7[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>7</span>
            <p style='margin-top: 8px'>Tạo Auth Cookie</p>
        </div>
    ]
    Step7 --> AuthController

    AuthController --- Step8[
        <div style='text-align: center'>
            <span style='display: inline-block; background-color: #6699cc !important; color:white; width: 28px; height: 28px; line-height: 28px; border-radius: 50%; font-weight: bold'>8</span>
            <p style='margin-top: 8px'>Trả về Response</p>
        </div>
    ]
    Step8 --> Client

    %% Styling
    style Client fill:#e6f3ff,stroke:#0066cc,stroke-width:2px
    style AuthController fill:#e6f3ff,stroke:#0066cc,stroke-width:2px
    style AuthService fill:#f0f8e6,stroke:#339933,stroke-width:2px
    style Firebase fill:#fcd9d9,stroke:#cc3333,stroke-width:2px
    style UserDB fill:#ffe6cc,stroke:#ff9900,stroke-width:2px
    style RoleDB fill:#ffe6cc,stroke:#ff9900,stroke-width:2px
    style SessionDB fill:#ffe6cc,stroke:#ff9900,stroke-width:2px
    style Step1 fill:transparent,stroke:transparent,stroke-width:1px
    style Step2 fill:transparent,stroke:transparent,stroke-width:1px
    style Step3 fill:transparent,stroke:transparent,stroke-width:1px
    style Step4 fill:transparent,stroke:transparent,stroke-width:1px
    style Step5 fill:transparent,stroke:transparent,stroke-width:1px
    style Step5_1 fill:transparent,stroke:transparent,stroke-width:1px
    style Step6 fill:transparent,stroke:transparent,stroke-width:1px
    style Step7 fill:transparent,stroke:transparent,stroke-width:1px
    style Step8 fill:transparent,stroke:transparent,stroke-width:1px

Bảng dữ liệu liên quan

erDiagram
    users {
        id bigint "Primary key"
        name string "User's full name"
        email string "User's email address (unique)"
        uid string "Firebase user ID (unique)"
        status int "Account status (0: Inactive, 1: Active)"
        is_first_login boolean "Flag indicating if user has completed first login"
    }
    roles {
        id bigint "Primary key"
        name string "Role display name"
        slug string "Role identifier for permissions (unique)"
    }
    user_roles {
        user_id bigint "Reference to users table (foreign key)"
        role_id bigint "Reference to roles table (foreign key)"
    }

    users ||--o{ user_roles : has
    roles ||--o{ user_roles : has

Tính năng của Module

Tính năng Mô tả Endpoint
Đăng nhập người dùng Xác thực email/password với Firebase token POST /api/v1/general/auth/login
Đăng ký người dùng Tạo tài khoản người dùng mới với thông tin công ty POST /api/v1/general/auth/register
Xác thực Google Xác thực và đăng ký dựa trên OAuth POST /api/v1/general/auth/google/register
Đặt lại mật khẩu Hệ thống khôi phục mật khẩu qua email POST /api/v1/general/auth/forgot
Đăng xuất người dùng Kết thúc session và xóa cookies GET /api/v1/general/auth/logout

Ghi chú bổ sung

  • Giới hạn tốc độ: Các lần thử đăng nhập bị giới hạn 5 lần mỗi phút cho mỗi địa chỉ IP
  • Token session hết hạn sau 24 giờ không hoạt động
  • Firebase authentication được sử dụng để quản lý người dùng an toàn
  • Tất cả các thao tác nhạy cảm yêu cầu xác thực lại
  • Xem xét triển khai 2FA cho người dùng admin trong các cập nhật tương lai