Skip to content

Add a React + Vite front end integrating the Castle browser SDK#4

Merged
bartes merged 4 commits into
mainfrom
react-integration
Jun 3, 2026
Merged

Add a React + Vite front end integrating the Castle browser SDK#4
bartes merged 4 commits into
mainfrom
react-integration

Conversation

@bartes

@bartes bartes commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

What & why

Adds a React + Vite + TypeScript front end under react/ that shows the idiomatic way to integrate the Castle browser SDK (@castleio/castle-js) in a React app, talking to the existing Express demo backend. The server-rendered Pug pages remain; this is an additional, self-contained example for SPA-style integrations.

How the integration works

  • Configure once: CastleProvider calls Castle.configure({ pk }) a single time, guarded against React StrictMode's double mount, and exposes a typed API through a useCastle() hook.
  • Fresh token per action: the login form calls createRequestToken() on submit and forwards it to POST /evaluate_login, which sends it to Castle's risk / filter endpoint.
  • Graceful degradation: with no VITE_CASTLE_PK the hook still resolves (empty token) and the UI surfaces a clear notice.
  • Custom events: trackCustom() wraps Castle.custom(...).

Changes

  • react/ Vite app: CastleProvider/useCastle, a LoginForm, a ResultPanel that renders the verdict/signals/raw payloads, and a typed API helper.
  • Vite dev server proxies the API routes to the Express backend (VITE_BACKEND_URL, default http://localhost:4006).
  • Shared dark-theme Tailwind tokens so it matches the rest of the demo visually.
  • A CI job that type-checks and builds the React app.
  • react/README.md plus a pointer from the main readme.

Known limitations

  • The demo posts to the existing backend; a real verdict requires valid castle_pk / castle_api_secret (otherwise the panel shows the API error, as expected).
  • The bundled SDK makes the production JS chunk large; left unsplit to keep the example simple.

Add a React + Vite + TypeScript app under react/ that demonstrates a proper
Castle browser SDK integration: a CastleProvider configures @castleio/castle-js
once and a useCastle() hook exposes createRequestToken() and custom-event
tracking. A login form mints a fresh request token on submit and evaluates it
through the existing Express backend (proxied via Vite in development).

- typed fetch + result panel rendering the verdict, signals and raw payloads
- shared dark-theme Tailwind tokens for a consistent look
- CI job to type-check and build the React app
- README documenting the setup
@bartes bartes force-pushed the react-integration branch from 63ab67c to fc27640 Compare June 3, 2026 14:43
bartes added 3 commits June 3, 2026 16:45
vite.config.ts reads process.env, which requires the Node type definitions
during the tsc build step in CI.
Bump react/react-dom to 19, their type definitions, and the Vite toolchain
(vite, @vitejs/plugin-react) to current majors.
Restructure the demo around the complete Castle user lifecycle. Every action
mints a fresh request token in the browser (castle.js) and forwards it to the
backend.

- add sign up ($registration -> risk/filter) as a server-rendered page
- add logout ($logout) via the non-blocking log endpoint
- the post-login React /account page now hosts profile update ($profile_update
  -> risk), a custom event, and logout — and is served by Express, mounted into
  a Pug shell that injects the publishable key and current user
- move the custom event out of the login page (it is post-login only)
- drop the Events (monitoring) API demo
- guard the server against unhandled rejections from the fire-and-forget log,
  so a failed background log can't crash the process
- tests for the new endpoints; readmes updated for the new flow
@bartes bartes merged commit 2668624 into main Jun 3, 2026
2 checks passed
@bartes bartes deleted the react-integration branch June 6, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant