# smakosh/roll-your-own-auth architecture

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

- Interactive diagram: https://gitdiagram.com/smakosh/roll-your-own-auth
- Repository: https://github.com/smakosh/roll-your-own-auth

## Overview

##### Project Type Analysis
This is an authentication framework/boilerplate project that provides different authentication implementation examples using various tech stacks. The project includes both client and server-side implementations with different authentication strategies (JWT, Sessions, SSO).

##### Main System Components

A. Documentation System
- Next.js-based documentation site (under /apps/docs)
- Authentication strategy guides (JWT, Sessions, SSO)

B. Example Implementations
##### REST API Examples
- Express + Prisma + JWT + SQLite
- Express + Prisma + Sessions + SQLite
- NestJS + Prisma + JWT + PostgreSQL
- Next.js + JWT
- Next.js + Sessions

C. CLI Tool
- Package for automation/scaffolding (under /packages/cli)

**┌─────────────────────────────────────────────────┐**
- Roll Your Own Auth                  │
- ┬─────────────────┬───────────────┤
- Documentation │    Examples     │    CLI Tool   │
- ┴─────────────────┴───────────────┘

**┌─────────────────────┐**
- Auth Strategies     │
- ┤
- JWT              │
- Sessions         │
- SSO              │
- 2FA              │
- ┘

**┌─────────────────┐     ┌─────────────────┐**
- Client Side     │     │ Server Side     │
- ┤     ├─────────────────┤
- Next.js       │     │ - Express       │
- NestJS        │
- ┘     └─────────────────┘

##### Important Elements

**User → Client → Auth Middleware → Server → Database**

b. Database Connections:
- SQLite and PostgreSQL database connections
- Prisma as ORM layer

c. Feature Components:
- Login/Signup flows
- Session management
- Token handling
- Middleware implementation
- Validation layers

##### Specific Implementation Details

For each example implementation, show:
- Route handlers
- Authentication middleware
- Database schema
- API endpoints
- Security measures

- Security boundaries and trust zones
- Error handling flows

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Main["Roll Your Own Auth"]
        Docs["Documentation System"]:::docs
        Examples["Example Implementations"]:::examples
        CLI["CLI Tool"]:::cli
    end

    subgraph Documentation["Documentation Layer"]
        JWT_Docs["JWT Docs"]:::docs
        Sessions_Docs["Sessions Docs"]:::docs
        SSO_Docs["SSO Docs"]:::docs
        TwoFA_Docs["2FA Docs"]:::docs
    end

    subgraph Examples_Layer["Implementation Examples"]
        subgraph Express_Examples["Express Examples"]
            Express_JWT["Express + JWT"]:::server
            Express_Sessions["Express + Sessions"]:::server
        end

        subgraph Next_Examples["Next.js Examples"]
            Next_JWT["Next.js + JWT"]:::client
            Next_Sessions["Next.js + Sessions"]:::client
        end

        subgraph Nest_Examples["NestJS Examples"]
            Nest_JWT["NestJS + JWT"]:::server
        end
    end

    subgraph Auth_Components["Authentication Components"]
        subgraph Controllers["Auth Controllers"]
            JWT_Controller["JWT Controllers"]:::component
            Sessions_Controller["Sessions Controllers"]:::component
            Nest_Controller["NestJS Controller"]:::component
        end

        subgraph Middleware["Auth Middleware"]
            JWT_Middleware["JWT Middleware"]:::component
            Sessions_Middleware["Sessions Middleware"]:::component
            Nest_Guard["NestJS Guard"]:::component
        end

        subgraph Forms["Client Forms"]
            JWT_Forms["JWT Forms"]:::component
            Sessions_Forms["Sessions Forms"]:::component
        end
    end

    subgraph Database["Database Layer"]
        JWT_Schema["JWT Schema"]:::db
        Sessions_Schema["Sessions Schema"]:::db
        Nest_Schema["NestJS Schema"]:::db
    end

    Docs --> Documentation
    Examples --> Examples_Layer
    Express_Examples --> Controllers
    Next_Examples --> Forms
    Nest_Examples --> Controllers
    Controllers --> Middleware
    Middleware --> Database

    %% Click Events
    click Docs "https://github.com/smakosh/roll-your-own-auth/tree/main/apps/docs"
    click JWT_Docs "https://github.com/smakosh/roll-your-own-auth/tree/main/apps/docs/pages/jwt"
    click Sessions_Docs "https://github.com/smakosh/roll-your-own-auth/tree/main/apps/docs/pages/sessions"
    click SSO_Docs "https://github.com/smakosh/roll-your-own-auth/tree/main/apps/docs/pages/sso"
    click TwoFA_Docs "https://github.com/smakosh/roll-your-own-auth/blob/main/apps/docs/pages/2fa.mdx"
    click Express_JWT "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-express-prisma-jwt-sqlite"
    click Express_Sessions "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-express-prisma-sessions-sqlite"
    click Next_JWT "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-next-jwt"
    click Next_Sessions "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-next-sessions"
    click Nest_JWT "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-nest-prisma-jwt-postgres"
    click JWT_Controller "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-express-prisma-jwt-sqlite/src/features/auth/controllers"
    click Sessions_Controller "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-express-prisma-sessions-sqlite/src/features/auth/controllers"
    click Nest_Controller "https://github.com/smakosh/roll-your-own-auth/blob/main/examples/rest-nest-prisma-jwt-postgres/src/auth/auth.controller.ts"
    click JWT_Middleware "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-express-prisma-jwt-sqlite/src/features/auth/middlewares"
    click Sessions_Middleware "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-express-prisma-sessions-sqlite/src/features/auth/middlewares"
    click Nest_Guard "https://github.com/smakosh/roll-your-own-auth/blob/main/examples/rest-nest-prisma-jwt-postgres/src/auth/auth.guard.ts"
    click CLI "https://github.com/smakosh/roll-your-own-auth/blob/main/packages/cli/src/index.ts"
    click JWT_Schema "https://github.com/smakosh/roll-your-own-auth/blob/main/examples/rest-express-prisma-jwt-sqlite/prisma/schema.prisma"
    click Sessions_Schema "https://github.com/smakosh/roll-your-own-auth/blob/main/examples/rest-express-prisma-sessions-sqlite/prisma/schema.prisma"
    click Nest_Schema "https://github.com/smakosh/roll-your-own-auth/blob/main/examples/rest-nest-prisma-jwt-postgres/prisma/schema.prisma"
    click JWT_Forms "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-next-jwt/src/app/(auth)/components"
    click Sessions_Forms "https://github.com/smakosh/roll-your-own-auth/tree/main/examples/rest-next-sessions/src/app/(auth)/components"

    %% Styling
    classDef docs fill:#3498db
    classDef examples fill:#2ecc71
    classDef cli fill:#9b59b6
    classDef server fill:#e74c3c
    classDef client fill:#f1c40f
    classDef component fill:#95a5a6
    classDef db fill:#34495e
```
