# ferdiebergado/react-netlify architecture

> Architecture diagram of the GitHub repository ferdiebergado/react-netlify, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ferdiebergado/react-netlify
- Repository: https://github.com/ferdiebergado/react-netlify

## Overview

#### Purpose and deployment model
A React/TypeScript starter application deployed as a Netlify site. It combines a Vite browser SPA with Netlify serverless API handlers and edge middleware for a session-backed Google OAuth authentication boundary. Netlify routing, redirects, headers, and edge-function mappings are centralized in `netlify.toml`.

#### Frontend entry and application shell
The browser entry point is `src/main.tsx`, which mounts the React application; `src/App.tsx` composes global providers and routing, while `src/page.tsx` is a top-level page boundary. Route-level screens include the authenticated dashboard and sign-in/fallback views:
- `src/pages/dashboard.tsx`
- `src/pages/signin-page.tsx`
- `src/pages/fallback.tsx`

Wouter provides client-side navigation, and TanStack Query supplies request caching/error integration through `src/lib/query-client.ts`.

#### Client auth boundary and navigation flow
Authentication state is fetched through the client API layer in `src/auth/api.ts` and exposed to UI through `src/auth/hooks.ts`. Route guards separate guest-only and user-required content:
- `src/auth/components/require-user.tsx`
- `src/auth/components/require-guest.tsx`
- `src/auth/components/signin-form.tsx`

The frontend calls Netlify API endpoints for sign-in/session identity/sign-out, rather than accessing database or OAuth credentials directly. Shared route names and API contracts should remain aligned via `shared/routes.ts` and `shared/schemas/user.ts`.

#### Serverless authentication API
Netlify Functions are the Node-side HTTP entry points under `/api`, delegating to backend services:
- `netlify/functions/signin.ts`
- `netlify/functions/oauthcallback.ts`
- `netlify/functions/me.ts`
- `netlify/functions/signout.ts`

Control flow is: sign-in initiates Google OAuth; Google redirects to the callback; the callback resolves/creates the user and session; subsequent `me` requests return the authenticated identity; sign-out removes the session. HTTP/cookie concerns are factored into `backend/http/cookie.ts` and `backend/http/middlewares.ts`.

#### Auth, session, and persistence domain
Backend domain code separates external OAuth, service logic, and persistence repositories:
- `backend/auth/google.ts`
- `backend/auth/service.ts`
- `backend/auth/repo.ts`
- `backend/session/service.ts`
- `backend/session/repo.ts`

Google OAuth is the external identity provider. Auth/session services are the core trust boundary: they translate OAuth identity into persisted users and opaque/session-cookie-backed access. Database connectivity is established in `backend/db.ts`, configured by `backend/config.ts`, and schema is defined in `init.sql`.

#### Edge enforcement layer
Netlify Edge Functions run before or around applicable requests to establish cross-cutting HTTP policy:
- `netlify/edge-functions/session.ts`
- `netlify/edge-functions/csrf.ts`
- `netlify/edge-functions/csp.ts`
- `netlify/edge-functions/json.ts`

This layer handles session context, CSRF protection, CSP headers, and JSON response conventions. Shared edge helpers reside in `netlify/edge-functions/_shared/utils.ts`. Changes here affect both SPA/API reachability and security behavior, independently of Node function execution.

#### Shared contracts, runtimes, and infrastructure
`shared/` is the explicit cross-runtime contract boundary used by browser, Node Functions, and edge code:
- `shared/schemas/base.ts`
- `shared/schemas/user.ts`
- `shared/types.ts`

Material runtimes are React 19 in the browser, Node.js 22+ for Netlify Functions/backend, and the Netlify Edge runtime. Persistence targets local SQLite for development/tests and remote Turso/libSQL in production, selected by environment settings. Required secrets include Google OAuth credentials and `APP_KEY`; database connectivity requires `DATABASE_URL` and, for Turso, `TURSO_AUTH_TOKEN`.

Main entry points: [`netlify.toml`](https://github.com/ferdiebergado/react-netlify/blob/main/netlify.toml) (Netlify routing), [`netlify/functions/signin.ts`](https://github.com/ferdiebergado/react-netlify/blob/main/netlify/functions/signin.ts) (Sign-in API), [`src/auth/api.ts`](https://github.com/ferdiebergado/react-netlify/blob/main/src/auth/api.ts) (Client auth state), [`src/App.tsx`](https://github.com/ferdiebergado/react-netlify/blob/main/src/App.tsx) (App shell & routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_browser["Browser SPA"]
  node_main["Browser entry<br/>React bootstrap<br/>[main.tsx]"]
  node_app["App shell &amp; routes<br/>React/Wouter<br/>[App.tsx]"]
  node_query["Query client<br/>TanStack Query<br/>[query-client.ts]"]
  node_client_auth["Client auth state<br/>API + hooks<br/>[api.ts]"]
  node_route_guards["Route guards<br/>auth components<br/>[require-user.tsx]"]
  node_dashboard["Dashboard<br/>protected screen<br/>[dashboard.tsx]"]
  node_signin["Sign-in screen<br/>guest screen<br/>[signin-page.tsx]"]
end

subgraph group_netlify["Netlify Runtime"]
  node_routing["Netlify routing<br/>deployment config<br/>[netlify.toml]"]
  node_edge_session{{"Edge session context<br/>Netlify Edge<br/>[session.ts]"}}
  node_edge_security{{"Edge security policy<br/>Netlify Edge<br/>[csrf.ts]"}}
  node_signin_fn["Sign-in API<br/>Netlify Function<br/>[signin.ts]"]
  node_callback_fn["OAuth callback API<br/>Netlify Function<br/>[oauthcallback.ts]"]
  node_session_api["Identity &amp; sign-out APIs<br/>Netlify Functions"]
end

subgraph group_backend["Auth Backend"]
  node_google(("Google OAuth<br/>external identity provider<br/>[google.ts]"))
  node_auth_service["Auth service<br/>trust boundary<br/>[service.ts]"]
  node_session_service["Session service<br/>trust boundary<br/>[service.ts]"]
  node_repos["User &amp; session repositories<br/>persistence access"]
  node_database[("SQLite / Turso database<br/>persistence target<br/>[db.ts]")]
end

subgraph group_shared["Shared Contracts"]
  node_routes_contract["Route contracts<br/>shared API routes<br/>[routes.ts]"]
  node_user_contract["User schema<br/>shared contract<br/>[user.ts]"]
end

node_main -->|"mounts"| node_app
node_app -->|"provides"| node_query
node_app -->|"routes through"| node_route_guards
node_route_guards -->|"permits user"| node_dashboard
node_route_guards -->|"permits guest"| node_signin
node_client_auth -->|"me / sign-out"| node_session_api
node_signin -->|"sign-in request"| node_signin_fn
node_query -->|"caches identity"| node_client_auth
node_routing -->|"maps requests"| node_edge_session
node_routing -->|"maps requests"| node_edge_security
node_routing -->|"routes /api"| node_signin_fn
node_routing -->|"routes callback"| node_callback_fn
node_routing -->|"routes /api"| node_session_api
node_edge_session -->|"session context"| node_signin_fn
node_edge_session -->|"session context"| node_session_api
node_edge_security -.->|"protects"| node_signin_fn
node_signin_fn -->|"redirects to"| node_google
node_google -->|"OAuth redirect"| node_callback_fn
node_callback_fn -->|"resolves identity"| node_auth_service
node_auth_service -->|"creates session"| node_session_service
node_session_api -->|"reads or revokes"| node_session_service
node_auth_service -->|"persists user"| node_repos
node_session_service -->|"persists session"| node_repos
node_repos -->|"queries"| node_database
node_client_auth -.->|"uses"| node_routes_contract
node_session_api -.->|"returns"| node_user_contract
node_client_auth -.->|"consumes"| node_user_contract

click node_main "https://github.com/ferdiebergado/react-netlify/blob/main/src/main.tsx"
click node_app "https://github.com/ferdiebergado/react-netlify/blob/main/src/App.tsx"
click node_query "https://github.com/ferdiebergado/react-netlify/blob/main/src/lib/query-client.ts"
click node_client_auth "https://github.com/ferdiebergado/react-netlify/blob/main/src/auth/api.ts"
click node_route_guards "https://github.com/ferdiebergado/react-netlify/blob/main/src/auth/components/require-user.tsx"
click node_dashboard "https://github.com/ferdiebergado/react-netlify/blob/main/src/pages/dashboard.tsx"
click node_signin "https://github.com/ferdiebergado/react-netlify/blob/main/src/pages/signin-page.tsx"
click node_routing "https://github.com/ferdiebergado/react-netlify/blob/main/netlify.toml"
click node_edge_session "https://github.com/ferdiebergado/react-netlify/blob/main/netlify/edge-functions/session.ts"
click node_edge_security "https://github.com/ferdiebergado/react-netlify/blob/main/netlify/edge-functions/csrf.ts"
click node_signin_fn "https://github.com/ferdiebergado/react-netlify/blob/main/netlify/functions/signin.ts"
click node_callback_fn "https://github.com/ferdiebergado/react-netlify/blob/main/netlify/functions/oauthcallback.ts"
click node_google "https://github.com/ferdiebergado/react-netlify/blob/main/backend/auth/google.ts"
click node_auth_service "https://github.com/ferdiebergado/react-netlify/blob/main/backend/auth/service.ts"
click node_session_service "https://github.com/ferdiebergado/react-netlify/blob/main/backend/session/service.ts"
click node_database "https://github.com/ferdiebergado/react-netlify/blob/main/backend/db.ts"
click node_routes_contract "https://github.com/ferdiebergado/react-netlify/blob/main/shared/routes.ts"
click node_user_contract "https://github.com/ferdiebergado/react-netlify/blob/main/shared/schemas/user.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_app,node_query,node_client_auth,node_route_guards,node_dashboard,node_signin toneBlue
class node_routing,node_edge_session,node_edge_security,node_signin_fn,node_callback_fn,node_session_api toneAmber
class node_google,node_auth_service,node_session_service,node_repos,node_database toneMint
class node_routes_contract,node_user_contract toneRose
```

## Components

### Browser SPA

- [Browser entry](https://github.com/ferdiebergado/react-netlify/blob/main/src/main.tsx): React bootstrap · `src/main.tsx`
- [App shell & routes](https://github.com/ferdiebergado/react-netlify/blob/main/src/App.tsx): React/Wouter · `src/App.tsx`
- [Query client](https://github.com/ferdiebergado/react-netlify/blob/main/src/lib/query-client.ts): TanStack Query · Caches API state and integrates request errors. · `src/lib/query-client.ts`
- [Client auth state](https://github.com/ferdiebergado/react-netlify/blob/main/src/auth/api.ts): API + hooks · `src/auth/api.ts`
- [Route guards](https://github.com/ferdiebergado/react-netlify/blob/main/src/auth/components/require-user.tsx): auth components · Separates guest-only and authenticated content. · `src/auth/components/require-user.tsx`
- [Dashboard](https://github.com/ferdiebergado/react-netlify/blob/main/src/pages/dashboard.tsx): protected screen · `src/pages/dashboard.tsx`
- [Sign-in screen](https://github.com/ferdiebergado/react-netlify/blob/main/src/pages/signin-page.tsx): guest screen · `src/pages/signin-page.tsx`

### Netlify Runtime

- [Netlify routing](https://github.com/ferdiebergado/react-netlify/blob/main/netlify.toml): deployment config · Maps redirects, headers, APIs, and edge functions. · `netlify.toml`
- [Edge session context](https://github.com/ferdiebergado/react-netlify/blob/main/netlify/edge-functions/session.ts): Netlify Edge · Establishes request session context before function handling. · `netlify/edge-functions/session.ts`
- [Edge security policy](https://github.com/ferdiebergado/react-netlify/blob/main/netlify/edge-functions/csrf.ts): Netlify Edge · Applies CSRF and CSP protections. · `netlify/edge-functions/csrf.ts`
- [Sign-in API](https://github.com/ferdiebergado/react-netlify/blob/main/netlify/functions/signin.ts): Netlify Function · Starts the Google OAuth flow. · `netlify/functions/signin.ts`
- [OAuth callback API](https://github.com/ferdiebergado/react-netlify/blob/main/netlify/functions/oauthcallback.ts): Netlify Function · Completes OAuth and creates authenticated access. · `netlify/functions/oauthcallback.ts`
- **Identity & sign-out APIs**: Netlify Functions · Returns the current user or removes the session.

### Auth Backend

- [Google OAuth](https://github.com/ferdiebergado/react-netlify/blob/main/backend/auth/google.ts): external identity provider · `backend/auth/google.ts`
- [Auth service](https://github.com/ferdiebergado/react-netlify/blob/main/backend/auth/service.ts): trust boundary · Maps OAuth identities to persisted users. · `backend/auth/service.ts`
- [Session service](https://github.com/ferdiebergado/react-netlify/blob/main/backend/session/service.ts): trust boundary · Issues and validates opaque cookie-backed sessions. · `backend/session/service.ts`
- **User & session repositories**: persistence access
- [SQLite / Turso database](https://github.com/ferdiebergado/react-netlify/blob/main/backend/db.ts): persistence target · Local SQLite or production libSQL is selected by environment. · `backend/db.ts`

### Shared Contracts

- [Route contracts](https://github.com/ferdiebergado/react-netlify/blob/main/shared/routes.ts): shared API routes · `shared/routes.ts`
- [User schema](https://github.com/ferdiebergado/react-netlify/blob/main/shared/schemas/user.ts): shared contract · Used across browser, functions, and edge runtimes. · `shared/schemas/user.ts`

## Connections

- Browser entry → App shell & routes: mounts
- App shell & routes → Query client: provides
- App shell & routes → Route guards: routes through
- Route guards → Dashboard: permits user
- Route guards → Sign-in screen: permits guest
- Client auth state → Identity & sign-out APIs: me / sign-out
- Sign-in screen → Sign-in API: sign-in request
- Query client → Client auth state: caches identity
- Netlify routing → Edge session context: maps requests
- Netlify routing → Edge security policy: maps requests
- Netlify routing → Sign-in API: routes /api
- Netlify routing → OAuth callback API: routes callback
- Netlify routing → Identity & sign-out APIs: routes /api
- Edge session context → Sign-in API: session context
- Edge session context → Identity & sign-out APIs: session context
- Edge security policy → Sign-in API: protects
- Sign-in API → Google OAuth: redirects to
- Google OAuth → OAuth callback API: OAuth redirect
- OAuth callback API → Auth service: resolves identity
- Auth service → Session service: creates session
- Identity & sign-out APIs → Session service: reads or revokes
- Auth service → User & session repositories: persists user
- Session service → User & session repositories: persists session
- User & session repositories → SQLite / Turso database: queries
- Client auth state → Route contracts: uses
- Identity & sign-out APIs → User schema: returns
- Client auth state → User schema: consumes
