How ferdiebergado/react-netlify works
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 Netlify routing · signin.ts Sign-in API · api.ts Client auth state · App.tsx App shell & routes
Components 20
Browser SPA
- Browser entry
src/main.tsx - App shell & routes
src/App.tsx - Query client · Caches API state and integrates request errors.
src/lib/query-client.ts - Client auth state
src/auth/api.ts - Route guards · Separates guest-only and authenticated content.
src/auth/components/require-user.tsx - Dashboard
src/pages/dashboard.tsx - Sign-in screen
src/pages/signin-page.tsx
Netlify Runtime
- Netlify routing · Maps redirects, headers, APIs, and edge functions.
netlify.toml - Edge session context · Establishes request session context before function handling.
netlify/edge-functions/session.ts - Edge security policy · Applies CSRF and CSP protections.
netlify/edge-functions/csrf.ts - Sign-in API · Starts the Google OAuth flow.
netlify/functions/signin.ts - OAuth callback API · Completes OAuth and creates authenticated access.
netlify/functions/oauthcallback.ts - Identity & sign-out APIs · Returns the current user or removes the session.
Auth Backend
- Google OAuth
backend/auth/google.ts - Auth service · Maps OAuth identities to persisted users.
backend/auth/service.ts - Session service · Issues and validates opaque cookie-backed sessions.
backend/session/service.ts - User & session repositories
- SQLite / Turso database · Local SQLite or production libSQL is selected by environment.
backend/db.ts
Shared Contracts
- Route contracts
shared/routes.ts - User schema · Used across browser, functions, and edge runtimes.
shared/schemas/user.ts
Connections 27
- 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