# pintoderian/next-directus-auth-ts architecture

> Architecture diagram of the GitHub repository pintoderian/next-directus-auth-ts, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pintoderian/next-directus-auth-ts
- Repository: https://github.com/pintoderian/next-directus-auth-ts

## Overview

##### Main Components
- Frontend UI: This consists of the React components for the dashboard, admin panel (under src/components/admin-panel), general UI components (located in src/components/ui), and theme or mode toggling components. These are responsible for rendering pages such as login, account management, posts, categories, tags, etc.
- Routing & Layout: Leveraging Next.js’ App Router structure. The “src/app” directory contains the route-based pages (root, dashboard, API endpoints, etc.) and layout files (layout.tsx) that define the overall structure.
- API & Authentication: Under src/app/api, most notably the NextAuth.js dynamic route ([...nextauth]) provides authentication endpoints. Also, the lib folder (src/lib/auth and associated files) supports session handling and validation logic.
- External Integrations:
- Directus: Found in src/services/directus.ts is responsible for connecting to and communicating with the Directus API for dynamic content.
- Third Party Libraries: NextAuth, Tailwind, shadcn UI components, and state management (using zustand via src/hooks/use-store.ts) make up the additional layers of functionality.

##### Relationships & Interactions
- The Browser/Client sends requests for pages (rendering components built with Next.js and Tailwind CSS).
- When authentication-related actions occur (like login), the client communicates with the API endpoints (src/app/api/auth/[...nextauth]) which interact with NextAuth.js flows using the library functions in src/lib/auth.
- Once authenticated, the UI components (dashboard and admin panel) retrieve user-specific or content data. They potentially call service functions (e.g., src/services/directus.ts) that interact with the Directus CMS.
- Global configuration elements like the Next.js configuration (next.config.js), tailwind, ESLint, and Prettier ensure consistency in building and styling the app.

##### Architectural Patterns & Design Principles
- File-Based Routing & Layout: The use of “src/app” and nested folders (like /dashboard and /api) indicates a clear separation between the user pages and API endpoints.
- Component Reusability: The modular structure with components stored under src/components (for both admin and common UI controls) reveals an emphasis on reusability and separation of concerns.
- Service and Utility Abstraction: Code in src/lib and src/services abstracts core functionalities (authentication, utility functions, and CMS integration) away from UI rendering, promoting a clean architecture.

##### Relevant Technologies and Frameworks
- Next.js is used for both the server-side rendering and API routes.
- Tailwind CSS provides the styling backbone.
- NextAuth.js is central for authentication.
- Directus is integrated as the CMS using the Directus SDK.
- Additional tooling includes Husky for Git hooks, ESLint/Prettier for code quality, and Zustand for lightweight state management.

##### Final Advice
- Authentication flow (login → NextAuth API → session management) and the integration with external content management (Directus).
- Modular separation (UI components, admin layouts, service layers) so every element’s role is unambiguous.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        Browser("Browser/Client"):::client
    end

    %% Routing & Layout Layer
    subgraph "Routing & Layout Layer"
        Routing("File-Based Routing & Layout"):::routing
    end

    %% UI Components Layer
    subgraph "UI Components Layer"
        Dashboard("Dashboard Pages & UI Layouts"):::ui
        UIComps("General UI Components"):::ui
        Admin("Admin Panel Components"):::ui
        Theme("Theme/Mode Toggling"):::ui
    end

    %% API & Authentication Layer
    subgraph "API & Authentication Layer"
        AuthAPI("Auth API Endpoint ([...nextauth])"):::api
        AuthHelpers("Authentication Helper Functions"):::api
    end

    %% External Services & State Management Layer
    subgraph "External & State Layer"
        Directus("Directus CMS Integration"):::external
        Zustand("Zustand State Management"):::state
    end

    %% Relationships
    Browser -->|"Routes"| Routing
    Routing -->|"renders"| Dashboard
    Routing -->|"renders"| UIComps
    Routing -->|"renders"| Admin
    Routing -->|"applies"| Theme
    Browser -->|"loginRequest"| AuthAPI
    AuthAPI -->|"uses"| AuthHelpers
    AuthAPI -->|"syncs"| Directus
    Dashboard -->|"stateMgmt"| Zustand
    UIComps -->|"stateMgmt"| Zustand
    Admin -->|"stateMgmt"| Zustand

    %% Click Events
    click Routing "https://github.com/pintoderian/next-directus-auth-ts/tree/main/src/app"
    click Dashboard "https://github.com/pintoderian/next-directus-auth-ts/tree/main/src/app/dashboard"
    click Dashboard "https://github.com/pintoderian/next-directus-auth-ts/blob/main/src/app/dashboard/layout.tsx"
    click Dashboard "https://github.com/pintoderian/next-directus-auth-ts/blob/main/src/app/page.tsx"
    click UIComps "https://github.com/pintoderian/next-directus-auth-ts/tree/main/src/components/ui"
    click Admin "https://github.com/pintoderian/next-directus-auth-ts/tree/main/src/components/admin-panel"
    click Theme "https://github.com/pintoderian/next-directus-auth-ts/blob/main/src/components/mode-toggle.tsx"
    click Theme "https://github.com/pintoderian/next-directus-auth-ts/blob/main/src/components/theme-provider.tsx"
    click AuthAPI "https://github.com/pintoderian/next-directus-auth-ts/blob/main/src/app/api/auth/[...nextauth]/route.ts"
    click AuthHelpers "https://github.com/pintoderian/next-directus-auth-ts/tree/main/src/lib/auth"
    click Directus "https://github.com/pintoderian/next-directus-auth-ts/blob/main/src/services/directus.ts"
    click Zustand "https://github.com/pintoderian/next-directus-auth-ts/blob/main/src/hooks/use-store.ts"

    %% Styles
    classDef client fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef routing fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef ui fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef api fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef external fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef state fill:#d1c4e9,stroke:#4a148c,stroke-width:2px;
```
