Visit my Portfolio! →

BFF.

Backend-for-frontend @sushrutalgs.ai

Built a Cloudflare Worker gateway that sits between the sushrutalgs.ai web and iOS apps and the AI backend, handling user authentication, per-user daily usage limits, and secure request forwarding so both apps talk to one trusted entry point. It runs live in staging and production and adds only about 14 milliseconds of overhead at the edge.

Source is private; sushrutalgs.ai is a live product. Happy to walk through the code or grant read access on request.

Links

  • live product↗
  • Request repo access→

Stack

  • TypeScript
  • Hono
  • Cloudflare Workers
  • jose (JWT)
  • Supabase
  • Vitest

System architecture. Tap to enlarge.

Overview

The BFF is the single trusted entry point for sushrutalgs.ai. Both the web and iOS apps call it instead of holding backend secrets or duplicating auth and billing logic. It verifies who the user is, enforces their daily usage limit, and forwards the request to the AI backend.

Approach

  • One gateway, two clients. It accepts an iOS bearer token or a web auth cookie and verifies Supabase JWTs at the edge, so neither client embeds the backend API key.
  • Atomic quota. A plan-aware daily limit is enforced through a single Supabase database function against the same row the apps read for their usage display, giving one source of truth.
  • Streaming pass-through. The query endpoint streams server-sent events back byte for byte, failing closed on quota or backend errors.

Results

Live in staging and production at about 14 ms of edge overhead, a roughly 33 KB gzipped bundle, and JWT verification at p95 around 0.13 ms. Quota enforcement measured about 9,200 operations per second on a single hot row, with verified fail-closed mapping and zero errors through 200 concurrent requests.

Engineering

TypeScript and Hono on Cloudflare Workers, with the jose library for JWT and JWKS verification, Supabase for auth and the quota function, and Cloudflare Access service tokens for upstream auth. Staging auto-deploys on push; production is gated by a release, so the iOS app's uptime is decoupled from web deploys.