How sergiodxa/remix-auth works
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)