Mobile and desktop apps (warpkit-apps)

Mobile and desktop apps (warpkit-apps)

warpkit can be the backend for the warpkit-apps template's mobile (Expo) and desktop (Electron) apps. The apps sign in with the same better-auth email code as the web app and call a small JSON API under /v1.

Turn it on

Set MOBILE_APP_ORIGINS to the apps' schemes, one per variant, comma separated:

MOBILE_APP_ORIGINS=warpkitapp-dev://,warpkitapp-preview://,warpkitapp://

Unset, no app can sign in and every /v1 route answers 404, so an app built from warpkit with no mobile client has neither. The schemes come from the app project's config.ts.

In the app project, point the apps at this server: make phone API=external in development (warpkit on port 3000), API_URL for builds.

How it works

  • src/server/auth.ts: @better-auth/expo's expo() plugin reads the expo-origin header the app sends; trustedOrigins (from MOBILE_APP_ORIGINS) lets that scheme pass the origin check. Sign-in (/api/auth/*) uses the usual cookies, kept by the app in secure storage. /v1 reads only Authorization: Bearer <session token> (better-auth's bearer() plugin); bearerOnly in mobile-api.server.ts strips any cookie first, so a browser's session cookie never authenticates a /v1 route. The web never sends a bearer token, so its sessions are unchanged.
  • src/routes/v1/*.ts: thin routes over src/server/mobile-api.server.ts.
    • GET /v1/hello: public greeting.
    • GET /v1/me: the signed-in user through requireUser, so banned and deleting users get 401.
    • DELETE /v1/me: deletes the account through deleteMyAccount (src/features/account/server/delete-account.server.ts), the same path as the settings page: rate limits, no deletion while impersonating, the deletion job. Answers { "deleted": true }. App stores require an app that creates accounts to offer this.
    • POST /v1/events: events from the app's offline outbox, sent with an Idempotency-Key header and delivered at least once. It checks the session, the key and the body, then answers 202 and drops the event. A project that stores events keys each one by its idempotency key inside withWriteLock, with its effects, and answers a key it has seen with duplicate: true.
    • GET /v1/notes, PUT /v1/notes/:id, DELETE /v1/notes/:id: the app's offline Notes, on the same notes table as the web (Notes).

Errors are JSON { "error": "<code>" }. The shapes are copied from warpkit-apps' packages/shared/src/api-contract.ts: change both together.

Notes

The app writes notes on the phone first and syncs them (warpkit-apps' docs/warpkit-apps/features/data.md). The rules in src/features/notes/server/notes-sync.server.ts are a copy of warpkit-apps' packages/shared/src/notes.ts: change both together.

  • Versions. Every edit, web or phone, bumps version. A phone edit that started from an older version answers 409 with the server's copy, and the app asks "Keep mine or use theirs". Pinning is web-only and bumps nothing.
  • Tombstones. A delete, web or phone, keeps the row with deleted = 1 and the title and text cleared, so the user's other devices learn of it from the changes feed. The web never shows one. Tombstones are not purged.
  • Changes feed. GET /v1/notes?since= returns notes with updatedAt >= since. Every write sets updated_at past every note's (NEXT_UPDATED_AT in notes.server.ts), not just past its own row's, so a since handed out never skips a later write: two writes in one millisecond, or a clock stepped back.
  • Replays. PUT carries an Idempotency-Key; last_write_key answers a retry of the last applied write unchanged.
  • Same gates as the web (notes-sync.service.server.ts): notes_enabled off answers 404; the note.created, note.updated and note.deleted rate limits (429, which the app retries); the plan's maxNotes on a create; withWriteLock; the audit events and the bell. A plan limit answers 422 with the message on title, since the contract has no plan-limit code.
  • The device's body is the table's content. Device limits are tighter than the web's (title 200, text 10 000): a longer web note syncs to the phone, and an edit there must shorten it.

Why /v1 and not /api/v1

The apps' contract uses /v1, the same paths as warpkit-apps' own API, so an app switches backends by URL alone. /api/v1 stays warpkit's web and API-key surface.

Origin check

React Native's fetch sends no Origin and no Sec-Fetch-Site; a browser always sends Sec-Fetch-Site. POST /v1/events, DELETE /v1/me and the note writes refuse any browser request that is not same-origin (isCrossSiteBrowserRequest), a second layer on top of /v1 ignoring cookies. src/start.ts's CSRF middleware covers server functions only, not these routes.