# awinogrodzki/next-firebase-auth-edge architecture

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

- Interactive diagram: https://gitdiagram.com/awinogrodzki/next-firebase-auth-edge
- Repository: https://github.com/awinogrodzki/next-firebase-auth-edge

## Overview

##### Main Components
- Core Authentication Module: This includes token generation, signature verification, issuing custom tokens, and related JWT operations. Look inside the “src/auth” directory for components like custom-token processing, signing, verifying, and error handling.
- Next.js Integration Layer: Represented by the “src/next” folder, this module handles API endpoints, middleware for authentication, token refresh mechanisms, and cookie-based session management.
- Cookie Management System: Under “src/next/cookies” and its subfolders, include cookie parsing, building, setting, expiration management, and removal. Each of these can be represented as individual components that interact with the Next.js layer.
- App Check Services: From “src/app-check”, capture the components that manage API-client integrations for app-check, token generation, and verification for the app’s health and security.

##### Relationships and Interactions
- Within the Next.js layer, the Cookie Management System is used to parse incoming requests and set secure cookies. Illustrate bidirectional data flow between the middleware and the cookie builder, parser, setter, and remover.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Each module (Auth, Next.js integration, Cookie management, App Check) is clearly separated into its own directory, reflecting modular design.
- Middleware Pattern: Emphasize how the integration with Next.js middleware abstracts away much of the boilerplate—allowing secure authentication and token management with minimal setup.
- Use of Edge/Node Abstractions: Highlight that the library’s design supports multiple runtimes by abstracting cryptographic operations through the Web Crypto API, ensuring compatibility with the Edge runtime.

##### Relevant Technologies and Libraries
- Next.js: As the framework in which the library is used, noted for both its legacy (getServerSideProps, API routes) and latest (App Router, Server Components) features.
- Firebase: Although not used directly in the code (since custom token handling replaces firebase-admin’s crypto usage), it is central to understanding the purpose of the library.
- jose: Used for JWT signing/verification.
- Web Crypto API: Critical for supporting Edge environments.
- Testing Tools: Jest configuration and test directories show a strong emphasis on integration and end-to-end testing which can be mentioned at the borders of each module if needed.

##### Essential Details
- Do not shy away from detailing sub-components like the various cookie handling utilities (builders, parsers, removers, setters) since they are critical for understanding how authentication state is managed in Next.js.
- Dependencies or flows related to external services (such as Firebase authentication verification endpoints) even if these are abstracted within the code.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Core Library Components
    subgraph "Core Library Components"
        A1["Auth Module – Token Generation & Verification"]:::core
        A2["Cookie Management – Builder/Parser/Setter/Remover"]:::core
        A3["App Check Service"]:::core
    end

    %% Integration Layer
    subgraph "Integration Layer"
        B1["Next.js Middleware – Auth Integration"]:::integration
    end

    %% Frontend/Client
    subgraph "Frontend/Client"
        C1["Documentation Site"]:::client
        C2["Example Next.js Apps"]:::client
    end

    %% External Dependencies
    subgraph "External Dependencies"
        D1["jose (JWT signing/verification)"]:::external
        D2["Web Crypto API (Edge support)"]:::external
        D3["Firebase (Authentication)"]:::external
    end

    %% Relationships
    B1 -->|"calls"| A1
    B1 -->|"manages"| A2
    A2 -->|"updates"| B1
    C1 -->|"uses"| B1
    C2 -->|"uses"| B1
    C2 -->|"calls"| A3
    A1 -->|"uses"| D1
    A1 -->|"uses"| D2
    A1 ---|"abstracts"| D3

    %% Click Events
    click A1 "https://github.com/awinogrodzki/next-firebase-auth-edge/tree/main/src/auth"
    click B1 "https://github.com/awinogrodzki/next-firebase-auth-edge/tree/main/src/next"
    click A2 "https://github.com/awinogrodzki/next-firebase-auth-edge/tree/main/src/next/cookies"
    click A3 "https://github.com/awinogrodzki/next-firebase-auth-edge/tree/main/src/app-check"
    click C1 "https://github.com/awinogrodzki/next-firebase-auth-edge/tree/main/docs"
    click C2 "https://github.com/awinogrodzki/next-firebase-auth-edge/tree/main/examples"

    %% Styles
    classDef core fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef integration fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef client fill:#FFFFCC,stroke:#000,stroke-width:2px;
    classDef external fill:#FFD580,stroke:#000,stroke-width:2px;
```
