# sergiodxa/remix-auth architecture

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

- Interactive diagram: https://gitdiagram.com/sergiodxa/remix-auth
- Repository: https://github.com/sergiodxa/remix-auth

## Overview

##### Project type and purpose
- This is an open-source authentication library (“Remix Auth”) for Remix and React Router applications.
- It provides a core Authenticator class and a Strategy abstraction so that end-users can plug in authentication flows (e.g., form–based, OAuth2, GitHub, custom).

##### File structure
- src/index.ts exports the core Authenticator.
- src/strategy.ts defines the abstract Strategy class.
- tests validate the core and strategy behavior.
- No frontend or backend folders—instead, it’s a toolkit to be dropped into a host Remix/React Router app.
- Supporting files: tsconfig.json, package.json, asset for documentation.

##### README for additional insights
- Core components: Authenticator<User> and Strategy<User, Options>.
- Uses Fetch API under the hood.
- Relies on a session storage (cookie-based or other) provided by the host app.
- Strategies are separate npm packages that extend the core Strategy class.

##### Main components and their interactions
a. Components:
- Host Application (Remix/React Router app)
- Authenticator core module
- Strategy abstract class
- Concrete Strategy implementations (e.g., FormStrategy, OAuth2Strategy, GitHubStrategy, CustomStrategy)
- Session Storage (cookie/session API from React Router)
- External Identity Providers (only for OAuth/OpenID strategies)
b. Relationships & data flow:
1. Browser sends HTTP request to Host Application (login route or protected route).
2. Host calls `authenticator.authenticate(strategyName, request)`.
3. Authenticator delegates to the selected Strategy implementation.
4. Strategy extracts credentials or triggers a redirect to External IdP.
5. Strategy calls its verify function to resolve/validate user.
6. On success, Authenticator returns a User object to the Host.
7. Host stores User in Session Storage → sets “Set-Cookie” header → response to Browser.
8. On subsequent protected requests, Host’s loader uses Session Storage to check authentication.
c. Architectural patterns:
- Strategy Pattern: pluggable authentication flows.
- Dependency Injection: user-provided verify function and session storage mechanism.
- Middleware-like flow: request → authenticator → strategy → verify → response.
d. Key technologies:
- TypeScript (strong typing for User and Options).
- Web Fetch API (Request, Response, Headers manipulation).
- React Router sessionStorage helpers or custom cookie/session handler.
- AsyncLocalStorage (optional advanced use).

##### Elements
- Core library (e.g., blue)
- Extension points/strategies (green)
- Host application (orange)
- External services (gray)

## Diagram (Mermaid)

```mermaid
flowchart TD
    Browser["Browser"]:::external
    Browser -->|HTTP Request| HostApp["Host Application (Remix App)"]:::host

    HostApp -->|"authenticate()<br/>strategyName, request"| Authenticator["Authenticator<User>"]:::core
    click Authenticator "https://github.com/sergiodxa/remix-auth/blob/main/src/index.ts"

    Authenticator -->|"delegate to"| StrategyInterface["Strategy<User, Options>"]:::core
    click StrategyInterface "https://github.com/sergiodxa/remix-auth/blob/main/src/strategy.ts"

    subgraph "Concrete Strategies"
        FormStrategy["FormStrategy"]:::strategy
        OAuth2Strategy["OAuth2Strategy"]:::strategy
        GitHubStrategy["GitHubStrategy"]:::strategy
        CustomStrategy["CustomStrategy"]:::strategy
    end

    StrategyInterface -->|implements| FormStrategy
    StrategyInterface -->|implements| OAuth2Strategy
    StrategyInterface -->|implements| GitHubStrategy
    StrategyInterface -->|implements| CustomStrategy

    FormStrategy -->|"extract credentials<br/>verify()"| VerifyFunc["verify()"]:::core
    OAuth2Strategy -->|"redirect"| ExternalIdP["External Identity Provider"]:::external
    ExternalIdP -->|"callback"| OAuth2Strategy

    VerifyFunc -->|"return User"| Authenticator
    Authenticator -->|"return User"| HostApp

    HostApp -->|"set-cookie<br/>store session"| SessionStorage["Session Storage"]:::host
    SessionStorage -->|"cookie header"| Browser

    Browser -->|Subsequent Request| HostApp
    HostApp -->|"get-session"| SessionStorage
    SessionStorage -->|"session data"| HostApp

    subgraph "Project Files"
        IndexTest["src/index.test.ts"]:::file
        StrategyTest["src/strategy.test.ts"]:::file
        Assets["assets/"]:::file
        TSConfig["tsconfig.json"]:::file
        Typedoc["typedoc.json"]:::file
        Package["package.json"]:::file
        ExportsScript["scripts/exports.ts"]:::file
    end

    click IndexTest "https://github.com/sergiodxa/remix-auth/blob/main/src/index.test.ts"
    click StrategyTest "https://github.com/sergiodxa/remix-auth/blob/main/src/strategy.test.ts"
    click Assets "https://github.com/sergiodxa/remix-auth/tree/main/assets/"
    click TSConfig "https://github.com/sergiodxa/remix-auth/blob/main/tsconfig.json"
    click Typedoc "https://github.com/sergiodxa/remix-auth/blob/main/typedoc.json"
    click Package "https://github.com/sergiodxa/remix-auth/blob/main/package.json"
    click ExportsScript "https://github.com/sergiodxa/remix-auth/blob/main/scripts/exports.ts"

    classDef core fill:#D0E6FF,stroke:#1E70BF,stroke-width:1px
    classDef strategy fill:#DFFFD0,stroke:#3B7A00,stroke-width:1px
    classDef host fill:#FFE5B4,stroke:#CC8400,stroke-width:1px
    classDef external fill:#E0E0E0,stroke:#666666,stroke-width:1px
    classDef file fill:#F7F7F7,stroke:#AAAAAA,stroke-dasharray: 2 2,stroke-width:1px
```
