# sc30gsw/next15-better-auth-tutorial architecture

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

- Interactive diagram: https://gitdiagram.com/sc30gsw/next15-better-auth-tutorial
- Repository: https://github.com/sc30gsw/next15-better-auth-tutorial

## Overview

##### Project Type and Purpose
- This is a Next.js application bootstrapped with create-next-app.
- It provides both frontend and backend functionalities: client-side rendering using Next.js pages and server-side operations via API routes (note the directories under “src/app/api” and “src/feature/auth/api”).
- It includes authentication features (with routes for sign-in, sign-up, and two-factor authentication) and a modular design with separate directories for “components”, “feature” (handling auth-specific logic), and libraries (for authentication, database access, etc.).

##### Analyzing the File Structure for Architectural Clues
- Top-level configuration and build scripts:
- Files like next.config.ts, package.json, tsconfig.json, and various configuration files (drizzle.config.ts, postcss.config.mjs) indicate standard Next.js setup.
- Database / ORM:
- The “drizzle” folder (with SQL migration files and JSON snapshots) and “src/lib/db” indicate that the project uses Drizzle ORM for database schema and migrations.
- API Endpoints and Business Logic:
- The “src/app/api” folder houses API routes (including [[...route]] for catch-all routes) and the “src/feature/auth/api” subdirectories for auth endpoints.
- The “src/feature/auth” folder contains actions, components, and hooks which collectively handle authentication flows.
- UI Components and Layout:
- The “src/app” folder has pages for sign-in, sign-up, two-factor authentication, and alerts while the “src/components/justd” contains UI components (buttons, cards, modals, etc.).
- Utility and Helper Modules:
- The “src/lib” directory includes authentication helpers and a database connector.
- Common utilities (like in “src/utils” and “src/hooks”) support reusable logic.

##### Elements
- Client-to-Frontend (user navigation and UI events).
- Frontend-to-API (data fetching, form submission).
- API-to-Database (query execution, transactions).
- API-to-Auth Module (session verification and authentication actions).
- Blue for UI/Frontend components,
- Green for server/API layers,
- Orange for authentication-related components,
- Grey for database and migration layers.

##### Putting It All Together
- Split the Next.js layer into two aspects: the UI (pages and UI components) and the server (API endpoints).
- Connections between the API endpoints and the database layer, and include a side module for authentication logic that interacts with both the frontend and backend.

## Diagram (Mermaid)

```mermaid
graph TD
    ClientBrowser["Client/Browser"]:::client
    Frontend["Next.js Frontend (src/app)"]:::frontend
    APILayer["API Layer (Routes)"]:::api
    AuthModule["Auth Module (Auth & Session)"]:::auth
    Database["Database (Drizzle ORM)"]:::database
    UIComponents["Reusable UI Components (justd)"]:::frontend
    Utilities["Utility & Helper Modules"]:::utility

    ClientBrowser -->|"navigates"| Frontend
    Frontend -->|"fetches_data"| APILayer
    APILayer -->|"queries"| Database
    APILayer -->|"authenticates"| AuthModule
    AuthModule -->|"session_management"| Database
    Frontend -->|"renders"| UIComponents
    Frontend -->|"utilizes"| Utilities
    APILayer -->|"utilizes"| Utilities
    AuthModule -->|"utilizes"| Utilities

    %% Click Events for Component Mapping
    click ClientBrowser "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/app/page.tsx"
    click ClientBrowser "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/app/layout.tsx"

    click Frontend "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/src/app/"
    click Frontend "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/src/app/@alert"

    click APILayer "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/app/api/[[...route]]/route.ts"
    click APILayer "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/app/api/auth/[...better-auth]/route.ts"
    click APILayer "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/src/feature/auth/api"

    click AuthModule "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/src/feature/auth"
    click AuthModule "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/lib/auth/auth-client.ts"
    click AuthModule "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/lib/auth/auth.ts"
    click AuthModule "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/lib/auth/get-server-session.ts"
    click AuthModule "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/lib/auth/mail.ts"
    click AuthModule "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/lib/auth/session-middleware.ts"

    click Database "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/drizzle"
    click Database "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/drizzle.config.ts"
    click Database "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/src/lib/db"

    click UIComponents "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/src/components/justd"

    click Utilities "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/lib/fetcher.ts"
    click Utilities "https://github.com/sc30gsw/next15-better-auth-tutorial/blob/main/src/lib/rpc.ts"
    click Utilities "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/src/utils"
    click Utilities "https://github.com/sc30gsw/next15-better-auth-tutorial/tree/main/src/hooks"

    %% Styles
    classDef client fill:#85C1E9,stroke:#2471A3,stroke-width:2px;
    classDef frontend fill:#AED6F1,stroke:#2471A3,stroke-width:2px;
    classDef api fill:#82E0AA,stroke:#27AE60,stroke-width:2px;
    classDef auth fill:#F5B041,stroke:#D35400,stroke-width:2px;
    classDef database fill:#D5DBDB,stroke:#AAB7B8,stroke-width:2px;
    classDef utility fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
```
