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'sexpo()plugin reads theexpo-originheader the app sends;trustedOrigins(fromMOBILE_APP_ORIGINS) lets that scheme pass the origin check. Sign-in (/api/auth/*) uses the usual cookies, kept by the app in secure storage./v1reads onlyAuthorization: Bearer <session token>(better-auth'sbearer()plugin);bearerOnlyinmobile-api.server.tsstrips any cookie first, so a browser's session cookie never authenticates a/v1route. The web never sends a bearer token, so its sessions are unchanged.src/routes/v1/*.ts: thin routes oversrc/server/mobile-api.server.ts.GET /v1/hello: public greeting.GET /v1/me: the signed-in user throughrequireUser, so banned and deleting users get 401.DELETE /v1/me: deletes the account throughdeleteMyAccount(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 anIdempotency-Keyheader 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 insidewithWriteLock, with its effects, and answers a key it has seen withduplicate: true.GET /v1/notes,PUT /v1/notes/:id,DELETE /v1/notes/:id: the app's offline Notes, on the samenotestable 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 = 1and 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 withupdatedAt >= since. Every write setsupdated_atpast every note's (NEXT_UPDATED_ATinnotes.server.ts), not just past its own row's, so asincehanded out never skips a later write: two writes in one millisecond, or a clock stepped back. - Replays.
PUTcarries anIdempotency-Key;last_write_keyanswers a retry of the last applied write unchanged. - Same gates as the web (
notes-sync.service.server.ts):notes_enabledoff answers 404; thenote.created,note.updatedandnote.deletedrate limits (429, which the app retries); the plan'smaxNoteson a create;withWriteLock; the audit events and the bell. A plan limit answers 422 with the message ontitle, since the contract has no plan-limit code. - The device's
bodyis the table'scontent. 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.