# stytchauth/stytch-node architecture

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

- Interactive diagram: https://gitdiagram.com/stytchauth/stytch-node
- Repository: https://github.com/stytchauth/stytch-node

## Overview

##### Main Components Organization

- B2C Authentication Services (Left)
- Shared Components (Center)
- B2B Authentication Services (Right)

##### Component Breakdown

A. B2C Authentication (Left Section):
- Related authentication methods:
- Magic Links Module
- OAuth Module
- Passwords Module
- OTP Module (SMS/Email/WhatsApp)
- WebAuthn Module
- Crypto Wallets Module
- Session Management
- User Management
- TOTP Module

B. Shared Components (Center Section):
- Base Client Implementation
- Error Handling
- Session Management
- M2M (Machine-to-Machine)
- Common Utilities (Base64, Environment)
- HTTP Client Layer (undici integration)

C. B2B Authentication (Right Section):
- Organizations Management
- Member Management
- RBAC System
- SSO Integration
- SAML
- OIDC
- Discovery Service
- MFA Components
- SCIM Integration

##### Connection and Flow Representation

- All authentication modules connecting to the central shared components
- Data flow between related modules (e.g., Passwords → Session Management)
- External service interactions (e.g., OAuth providers, SAML providers)

##### Implementation Details

Technical annotations showing:
- TypeScript types and interfaces
- API endpoint groupings
- Error handling paths
- Configuration options

##### Notes Section

Callouts for:
- Security considerations
- Configuration requirements
- Runtime requirements
- Type system integration

- Clear separation between B2B and B2C features
- Modular architecture allowing independent use of features
- Shared infrastructure components
- Type safety throughout the system
- Error handling paths
- External service integration points

Keep the abstraction level balanced:
- Enough detail to understand the component relationships
- Avoid overwhelming with implementation details
- Service boundaries and interactions
- Extension points and customization options

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph B2C["B2C Authentication Services"]
        direction TB
        ML["Magic Links"]:::b2c
        OAuth["OAuth"]:::b2c
        PWD["Passwords"]:::b2c
        OTP["OTP Module"]:::b2c
        WA["WebAuthn"]:::b2c
        CW["Crypto Wallets"]:::b2c
        SM["Session Management"]:::b2c
        UM["User Management"]:::b2c
        TOTP["TOTP"]:::b2c
    end

    subgraph Shared["Shared Components"]
        direction TB
        BC["Base Client"]:::shared
        EH["Error Handling"]:::shared
        SMS["Session Management"]:::shared
        M2M["M2M"]:::shared
        CU["Common Utilities"]:::shared
        HTTP["HTTP Client Layer"]:::shared
    end

    subgraph B2B["B2B Authentication Services"]
        direction TB
        ORG["Organizations Management"]:::b2b
        MM["Member Management"]:::b2b
        RBAC["RBAC System"]:::b2b
        SSO["SSO Integration"]:::b2b
        DS["Discovery Service"]:::b2b
        MFA["MFA Components"]:::b2b
        SCIM["SCIM Integration"]:::b2b
        
        subgraph SSO_Components["SSO Components"]
            SAML["SAML"]:::b2b
            OIDC["OIDC"]:::b2b
        end
    end

    %% Connections
    ML & OAuth & PWD & OTP & WA & CW & SM & UM & TOTP --> BC
    ORG & MM & RBAC & SSO & DS & MFA & SCIM --> BC
    BC --> HTTP
    BC --> EH
    BC --> SMS
    BC --> CU

    %% Click Events
    click ML "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/magic_links.ts"
    click OAuth "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/oauth.ts"
    click PWD "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/passwords.ts"
    click OTP "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/otps.ts"
    click WA "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/webauthn.ts"
    click CW "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/crypto_wallets.ts"
    click SM "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/sessions.ts"
    click UM "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/users.ts"
    click TOTP "https://github.com/stytchauth/stytch-node/blob/main/lib/b2c/totps.ts"
    click BC "https://github.com/stytchauth/stytch-node/blob/main/lib/shared/client.ts"
    click EH "https://github.com/stytchauth/stytch-node/blob/main/lib/shared/errors.ts"
    click SMS "https://github.com/stytchauth/stytch-node/blob/main/lib/shared/sessions.ts"
    click CU "https://github.com/stytchauth/stytch-node/blob/main/lib/shared/base64.ts"
    click ORG "https://github.com/stytchauth/stytch-node/blob/main/lib/b2b/organizations.ts"
    click MM "https://github.com/stytchauth/stytch-node/blob/main/lib/b2b/organizations_members.ts"
    click RBAC "https://github.com/stytchauth/stytch-node/blob/main/lib/b2b/rbac.ts"
    click SAML "https://github.com/stytchauth/stytch-node/blob/main/lib/b2b/sso_saml.ts"
    click OIDC "https://github.com/stytchauth/stytch-node/blob/main/lib/b2b/sso_oidc.ts"
    click SCIM "https://github.com/stytchauth/stytch-node/blob/main/lib/b2b/scim.ts"
    click MFA "https://github.com/stytchauth/stytch-node/blob/main/lib/b2b/mfa.ts"
    click DS "https://github.com/stytchauth/stytch-node/blob/main/lib/b2b/discovery.ts"

    %% Styles
    classDef b2c fill:#2374ab,stroke:#000,color:#fff
    classDef shared fill:#57886C,stroke:#000,color:#fff
    classDef b2b fill:#7D4E57,stroke:#000,color:#fff

    %% Legend
    subgraph Legend
        L1["B2C Components"]:::b2c
        L2["Shared Components"]:::shared
        L3["B2B Components"]:::b2b
    end
```
