# leerob/next-saas-starter architecture

> Architecture diagram of the GitHub repository leerob/next-saas-starter, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/leerob/next-saas-starter
- Repository: https://github.com/leerob/next-saas-starter

## Overview

##### Main Components

a) Client-Side Layer:
- Next.js Frontend (App Router)
- Marketing Pages (/, /pricing)
- Dashboard Routes (/dashboard/*)
- Authentication Pages (/sign-in, /sign-up)
- UI Components (shadcn/ui)

b) Server-Side Layer:
- Next.js API Routes
- Server Actions
- Authentication Middleware
- Database Layer
- Payment Integration

##### Frontend Architecture
- Public Routes
- Landing Page
- Pricing Page

- Protected Routes
- Dashboard
- Settings
- Security
- Activity

- Auth Routes
- Sign In
- Sign Up

##### Middleware Layer
- Global Auth Middleware
- Route Protection
- Schema Validation

##### Backend Services
- Authentication Service
- JWT Management
- Session Handling

- Database Layer (PostgreSQL)
- User Management
- Team Management
- Activity Logging

- Payment Integration
- Stripe Checkout
- Webhook Handler
- Customer Portal

##### Key Relationships to Highlight

a) Authentication Flow:
- How auth middleware protects dashboard routes
- JWT flow from login to protected routes
- Demonstrate session management

b) Payment Flow:
- Stripe Checkout integration
- Webhook handling for subscription events
- Customer portal integration

c) Data Flow:
- Server Actions for CRUD operations
- Database queries and migrations
- Activity logging system

##### Important Technical Details

Specific implementations:
- Next.js App Router structure
- Drizzle ORM integration
- Server Components vs Client Components
- API routes for Stripe integration
- Database schema relationships

##### Additional Notes

- Version numbers for key technologies
- Authentication strategy (JWT + Cookies)
- RBAC system (Owner/Member roles)
- Activity logging system's integration points

Remember to include a small section showing the development tools and utilities (migrations, seeding, etc.) as a separate panel, as these are important for the complete system understanding but not part of the runtime architecture.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Frontend Layer"
        PublicRoutes["Public Routes"]:::frontend
        ProtectedRoutes["Protected Routes"]:::frontend
        AuthRoutes["Auth Routes"]:::frontend
        
        subgraph "Public Pages"
            Landing["Landing Page"]:::frontend
            Pricing["Pricing Page"]:::frontend
        end
        
        subgraph "Dashboard Pages"
            Dashboard["Dashboard"]:::frontend
            Settings["Settings"]:::frontend
            Security["Security"]:::frontend
            Activity["Activity"]:::frontend
        end
        
        subgraph "Auth Pages"
            SignIn["Sign In"]:::frontend
            SignUp["Sign Up"]:::frontend
        end
        
        subgraph "UI Components"
            BaseUI["Base Components"]:::frontend
            Button["Button"]:::frontend
            Input["Input"]:::frontend
            Card["Card"]:::frontend
            Dropdown["Dropdown"]:::frontend
        end
    end
    
    subgraph "Middleware Layer"
        GlobalAuth["Global Auth Middleware"]:::middleware
        RouteProtection["Route Protection"]:::middleware
        SchemaValidation["Schema Validation"]:::middleware
    end
    
    subgraph "Backend Services"
        AuthService["Authentication Service"]:::backend
        DBLayer["Database Layer"]:::backend
        PaymentService["Payment Integration"]:::backend
        
        subgraph "Auth Components"
            JWTManagement["JWT Management"]:::backend
            SessionHandling["Session Management"]:::backend
        end
        
        subgraph "Database Components"
            UserManagement["User Management"]:::backend
            TeamManagement["Team Management"]:::backend
            ActivityLogging["Activity Logging"]:::backend
        end
        
        subgraph "Payment Components"
            StripeCheckout["Stripe Checkout"]:::payment
            WebhookHandler["Webhook Handler"]:::payment
            CustomerPortal["Customer Portal"]:::payment
        end
    end
    
    %% Relationships
    PublicRoutes --> Landing & Pricing
    ProtectedRoutes --> Dashboard & Settings & Security & Activity
    AuthRoutes --> SignIn & SignUp
    
    GlobalAuth --> RouteProtection
    RouteProtection --> ProtectedRoutes
    
    AuthService --> JWTManagement & SessionHandling
    DBLayer --> UserManagement & TeamManagement & ActivityLogging
    PaymentService --> StripeCheckout & WebhookHandler & CustomerPortal
    
    %% Click Events
    click Dashboard "https://github.com/leerob/next-saas-starter/tree/main/app/(dashboard)/dashboard/*"
    click Activity "https://github.com/leerob/next-saas-starter/blob/main/app/(dashboard)/dashboard/activity/page.tsx"
    click Security "https://github.com/leerob/next-saas-starter/blob/main/app/(dashboard)/dashboard/security/page.tsx"
    click Settings "https://github.com/leerob/next-saas-starter/blob/main/app/(dashboard)/dashboard/general/page.tsx"
    click AuthRoutes "https://github.com/leerob/next-saas-starter/tree/main/app/(login)/*"
    click GlobalAuth "https://github.com/leerob/next-saas-starter/blob/main/lib/auth/middleware.ts"
    click SessionHandling "https://github.com/leerob/next-saas-starter/blob/main/lib/auth/session.ts"
    click StripeCheckout "https://github.com/leerob/next-saas-starter/blob/main/app/api/stripe/checkout/route.ts"
    click WebhookHandler "https://github.com/leerob/next-saas-starter/blob/main/app/api/stripe/webhook/route.ts"
    click PaymentService "https://github.com/leerob/next-saas-starter/blob/main/lib/payments/actions.ts"
    click DBLayer "https://github.com/leerob/next-saas-starter/blob/main/lib/db/schema.ts"
    click UserManagement "https://github.com/leerob/next-saas-starter/blob/main/lib/db/queries.ts"
    click BaseUI "https://github.com/leerob/next-saas-starter/tree/main/components/ui/*"
    click Button "https://github.com/leerob/next-saas-starter/blob/main/components/ui/button.tsx"
    click Input "https://github.com/leerob/next-saas-starter/blob/main/components/ui/input.tsx"
    click Card "https://github.com/leerob/next-saas-starter/blob/main/components/ui/card.tsx"
    click Dropdown "https://github.com/leerob/next-saas-starter/blob/main/components/ui/dropdown-menu.tsx"
    click Pricing "https://github.com/leerob/next-saas-starter/blob/main/app/(dashboard)/pricing/page.tsx"
    click SignIn "https://github.com/leerob/next-saas-starter/blob/main/app/(login)/sign-in/page.tsx"
    click SignUp "https://github.com/leerob/next-saas-starter/blob/main/app/(login)/sign-up/page.tsx"
    
    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef middleware fill:#f1c40f,stroke:#f39c12,color:black
    classDef backend fill:#2ecc71,stroke:#27ae60,color:white
    classDef payment fill:#e74c3c,stroke:#c0392b,color:white
    
    %% Legend
    subgraph Legend
        Frontend["Frontend Component"]:::frontend
        Middleware["Middleware Component"]:::middleware
        Backend["Backend Service"]:::backend
        Payment["Payment Service"]:::payment
    end
```
