# The mobile app (/docs/starter/mobile-app)

```npm
npx @clubedge/create-clubedge-app my-app --framework vite --api hono --shell capacitor
```

`--shell capacitor` packages the [React + Vite frontend](../cli/frontend-and-api.mdx) as an Android and iOS app with [Capacitor](https://capacitorjs.com) 8, in `apps/shell`. The app's pages load from the device: they are the web app, built in Vite's `app` mode, and they call the API through your deployed website. It needs CLI 0.42.0 or newer (Starter v0.27.0). For an app with native screens of its own, see [The native app](./native-app.mdx) (`--mobile expo`); a project can have both.

<Callout>
  Try the Starter's app on an Android phone: download [clubedge-demo.apk](https://github.com/Clubedge/clubedge-starter/releases/latest/download/clubedge-demo.apk) from the latest Starter release and open it, allowing installs from unknown sources when Android asks. It is the app's [demo build](#the-demo), with sample data that stays on the phone, no account, and no server behind it. iPhones install apps only from the App Store or TestFlight, so there is no file for them.
</Callout>

## Choose it [#choose-it]

A shell packages a frontend's static files, so `--shell capacitor` needs `--framework vite`, which needs its API, `--api hono` or `--api fastapi`. Next.js and TanStack Start render their pages on their own server, so they can't be packaged, and the CLI refuses them:

```text
Next.js renders pages on its server, so Capacitor cannot package it: choose a frontend, or --shell none.
```

Interactive runs ask **Package the web app as a mobile app?** after the frontend and its API, with Capacitor and None, the default. In the [builder](https://starter.clubedge.live/builder?preset=web-mobile), the Mobile app row appears once React + Vite is picked. The project's `package.json` records the choice in `"apps"`, as `"shell": "capacitor"`.

A project created without it can add it later, and drop it again, with CLI 0.46.0 or newer: `pnpm clubedge add shell capacitor`, or `pnpm clubedge remove shell` ([Change a project](../cli/change-a-project.mdx#apps-installed-on-devices)).

## What you get [#what-you-get]

<Files>
  <Folder name="apps" defaultOpen>
    <Folder name="web" />
    <Folder name="api" />
    <Folder name="shell" defaultOpen>
      <Folder name="scripts" />
      <File name="capacitor.config.ts" />
      <File name=".env.local" />
      <File name="package.json" />
    </Folder>
  </Folder>
  <Folder name="packages" defaultOpen>
    <Folder name="mobile" />
    <Folder name="contract" />
    <File name="… one package per module you chose" />
  </Folder>
</Files>

- **`apps/shell`**: the app, package `@clubedge/shell`. `capacitor.config.ts` sets its id and name, `scripts/build.mjs` builds the web app for it and copies the build into the native projects, and `.env.local` holds `WEB_URL`. `android/` and `ios/` appear on the first build.
- **`packages/mobile`**: the app's own code, which the web app loads only in its build for the app: the session's storage on the device, the app's version, the update notice, and the demo's fakes. The website's build has no Capacitor.
- **`apps/web/dist-app`**: the web app's build for the app. Git ignores it.

## Requirements [#requirements]

- [Android Studio](https://developer.android.com/studio) 2025.2.1 or newer, for Android. It brings the Java 21 that Capacitor 8 builds with.
- A Mac with [Xcode](https://developer.apple.com/xcode/) 26 or newer, for iOS. The iOS project uses Swift Package Manager rather than CocoaPods.

Capacitor's [environment setup](https://capacitorjs.com/docs/getting-started/environment-setup) has the details.

## Set it up [#set-it-up]

<Steps>
<Step>

### Set the app's id [#set-the-apps-id]

In `apps/shell/capacitor.config.ts`, replace the generated `com.example.<project>` with a reverse-domain id you own, such as `com.yourcompany.app`. It becomes permanent once the Android and iOS projects exist and a store lists the app, so set it before the first build. The app's name comes from `apps/web/src/config/site.json`.

</Step>
<Step>

### Point it at your website [#point-it-at-your-website]

Set `WEB_URL` to the website's public `https://` address: where the React + Vite frontend is deployed. The app sends its session there, so the build refuses any other scheme, a path, or a query.

```dotenv title="apps/shell/.env.local"
WEB_URL=https://app.example.com
```

</Step>
<Step>

### Build and open it [#build-and-open-it]

```sh
pnpm --filter @clubedge/shell android   # or ios, on a Mac
```

It builds the web app for the app, creates `apps/shell/android` (or `apps/shell/ios`) on the first run, copies the build into it, and opens Android Studio (or Xcode), where you run the app on a device or an emulator. Commit `android/` and `ios/`: from then on they are your app's own, with its icons, permissions, and signing settings.

</Step>
</Steps>

## Commands [#commands]

| Command | What it does |
| --- | --- |
| `pnpm --filter @clubedge/shell android` | Build the app for Android and open it in Android Studio. The first run creates `apps/shell/android`. |
| `pnpm --filter @clubedge/shell ios` | The same for iOS and Xcode, on a Mac. The first run creates `apps/shell/ios`. |
| `pnpm --filter @clubedge/shell sync` | Copy a new build of the web app into the native projects. |

Add `--demo` to any of them for the [demo](#the-demo). After changing the web app, run `sync`. Release builds and store listings are made in Android Studio and Xcode, as Capacitor's guides for [Google Play](https://capacitorjs.com/docs/android/deploying-to-google-play) and the [App Store](https://capacitorjs.com/docs/ios/deploying-to-app-store) describe.

### Live reload [#live-reload]

To see changes on a device as you make them, the app can load its pages from your computer's dev server instead. After a first `android` (or `ios`) build, start the web app in app mode, then run the app on a device or an emulator on the same network:

```sh
pnpm --filter @clubedge/web dev --mode app
pnpm --filter @clubedge/shell exec cap run android --live-reload
```

The app then loads `http://<your computer's address>:3000`; change it with `--host` and `--port`, and use `ios` for iOS. With Better Auth, add that address to `MOBILE_ORIGINS` in `apps/web/.env.local` first, so Better Auth accepts the app's sign-in. Ctrl+C stops it. The installed app keeps loading its pages from your computer until you install a normal build again.

## How it differs from the website [#how-it-differs-from-the-website]

- **Its origins.** Its pages load from `https://localhost` on Android and `capacitor://localhost` on iOS. The website's server answers these origins' calls to `/api` with CORS headers: they are its `MOBILE_ORIGINS`, `capacitor://localhost,https://localhost` by default, which you only set to change them.
- **Its session.** The website's cookies never reach the app, so it keeps its session in the device's storage, private to the app, and sends it with each call.
- **Emailed links**, such as sign-up confirmations and password resets, open the website, never the app. After confirming an address, sign in again in the app.
- **No payments.** App stores require their own in-app purchases for digital goods, so the app has no billing card: subscriptions stay on the website.
- **Its settings.** Its build reads `apps/web/.env.local` too, and the settings in `apps/shell/.env.local` win. Give it the deployed website's public `VITE_*` settings where development uses others.

### Sign-in [#sign-in]

<Tabs items={['Supabase Auth', 'Better Auth', 'Firebase', 'Appwrite']} groupId="auth" persist>
  <Tab value="Supabase Auth">
    Supabase keeps the app's session in the device's storage. With `--infra supabase`, the local Supabase runs on your computer, out of the device's reach: set your Supabase project's `VITE_SUPABASE_URL` and `VITE_SUPABASE_PUBLISHABLE_KEY` in `apps/shell/.env.local`.
  </Tab>
  <Tab value="Better Auth">
    Better Auth signs the app in with bearer tokens, from its bearer plugin, and only the app receives them: the website keeps its HttpOnly cookie. The API checks the session from the token as it does from the cookie. Better Auth trusts the `MOBILE_ORIGINS` for sign-in.
  </Tab>
  <Tab value="Firebase">
    Firebase Authentication keeps the app's session in the WebView's IndexedDB, and starts without the frame its pop-up and redirect sign-in need, which would never load in an iOS WebView. The Auth Emulator runs on your computer, out of the device's reach, so `apps/shell/.env.local` leaves `VITE_FIREBASE_AUTH_EMULATOR_URL` empty: the app signs in with your Firebase project.
  </Tab>
  <Tab value="Appwrite">
    In your Appwrite project, add a **Web** platform with the hostname `localhost` too: the app's pages use it. Appwrite's SDK keeps the app's session itself.
  </Tab>
</Tabs>

## Updates [#updates]

An installed app keeps the version it shipped with, often for months. It asks the API's `GET /api/app-version` which versions the API serves, an answer that is never cached, and compares its own. It asks when it starts, and from Starter v0.29.0 also when it comes back to the foreground (at most every 15 minutes) and whenever the API refuses its version ([Apps the API refuses](../cli/frontend-and-api.mdx#apps-the-api-refuses)):

- Below `APP_MINIMUM_VERSION`, it shows **Update required**, a screen that keeps the app closed, with an **Update** button that opens `APP_UPDATE_URL` in the phone's browser. From Starter v0.29.0, the app remembers it, so it holds when the app next starts offline, until it is updated.
- Below `APP_NEXT_MINIMUM_VERSION` (Starter v0.29.0 or newer), a notice above the page says the date the app stops working, with the same button. **Not now** hides it until the next day.
- Below `APP_LATEST_VERSION` only, a notice offers the update, and **Not now** hides it until a newer version is out.
- When the API can't be reached, or doesn't know `/api/app-version` yet, nothing new shows: a remembered **Update required** still holds, and otherwise the app works as usual.

Set them in `apps/api/.env.local`, or in the API's environment in production. Each is `null` until set:

| Variable | Meaning |
| --- | --- |
| `APP_MINIMUM_VERSION` | The oldest app version the API still serves. |
| `APP_LATEST_VERSION` | The newest released version. It can't be older than the minimum. |
| `APP_UPDATE_URL` | Where to get the update: an `https://` link to the store listing or a download page. |
| `APP_NEXT_MINIMUM_VERSION`, `APP_NEXT_MINIMUM_VERSION_FROM` | A minimum announced for a date, such as `2026-11-01T00:00:00Z`, which the API requires from then. Starter v0.29.0 or newer. |

From Starter v0.29.0, each can be set for one platform with `_IOS` or `_ANDROID`, such as `APP_UPDATE_URL_IOS`: see [Apps installed on phones](../cli/frontend-and-api.mdx#apps-installed-on-phones).

Versions are `MAJOR.MINOR.PATCH`, such as `1.4.0`. Other values fail the request rather than reach the apps, and `pnpm check:env` lists them before a deploy.

From Starter v0.30.0, the app's version is `version` in `apps/shell/version.json`, which each build writes into both native projects: see [Store releases](#store-releases). Before, it was `versionName` in `apps/shell/android/app/build.gradle` and `MARKETING_VERSION` in Xcode.

Installed apps also keep calling the API contract they shipped with, so CI fails a pull request that breaks it: see [Apps installed on phones](../cli/frontend-and-api.mdx#apps-installed-on-phones).

## Store releases [#store-releases]

From Starter v0.30.0, the app's version, which the stores show and the update notice compares, is in `apps/shell/version.json`. Each build writes it into both native projects, with the build number the stores order builds by, made from it: 1.4.2 is build 1004002. A build never takes a native project's version back: after upgrading from an earlier Starter, whose native projects held the version, the first build stops and names the `set-version` command that carries it over. Set a newer one for each release:

```sh
pnpm --filter @clubedge/shell set-version 1.5.0
```

`pnpm --filter @clubedge/shell release android` (or `ios`) builds the release, signed, and copies it to `apps/shell/release/`: an App Bundle for Google Play, which Gradle builds and the JDK's `jarsigner` signs, or an IPA for the App Store, which Capacitor builds and Xcode signs. It signs with the settings in `apps/shell/.env.local`:

| Platform | Settings |
| --- | --- |
| Android | Your upload key: `ANDROID_KEYSTORE_PATH`, `ANDROID_KEYSTORE_PASSWORD`, `ANDROID_KEY_ALIAS`, and `ANDROID_KEY_PASSWORD`. Make the keystore once with `keytool -genkeypair -v -keystore upload.keystore -alias upload -keyalg RSA -keysize 2048 -validity 10000`, and keep it outside the repository. |
| iOS, on a Mac | `IOS_TEAM_ID`, for Xcode's automatic signing; to sign manually, `IOS_SIGNING_CERTIFICATE` and `IOS_PROVISIONING_PROFILE` too. |

The **Mobile app release** workflow (`.github/workflows/shell-release.yml`, run from the Actions tab) does the same on GitHub's runners, keeps each build as the run's artifact, and can upload the iOS build to TestFlight. It builds for the website in the `WEB_URL` repository variable, with the signing settings as repository secrets: the keystore and the certificate as base64 (`ANDROID_KEYSTORE_BASE64`, `IOS_CERTIFICATE_BASE64`, `IOS_PROVISIONING_PROFILE_BASE64`), and an App Store Connect API key for TestFlight. The project's SETUP.md lists each one.

Google Play takes an app's first upload in its console. Once the stores publish a release, set the API's `APP_LATEST_VERSION` to it ([Updates](#updates)).

## Push notifications [#push-notifications]

With `--push native` (Starter v0.30.0 or newer), the app's settings page gets a Notifications card: a switch, and a test notification. The API sends straight to FCM and APNs, and each build sets push up in the native projects. See [Push notifications](./modules/push.mdx).

## The demo [#the-demo]

```sh
pnpm --filter @clubedge/shell android --demo   # or ios --demo
```

The demo build is for trying the app, or for store screenshots: it needs no server and no `WEB_URL`. Sign-in and the API answer from memory on the device, with fakes from `packages/mobile/src/demo`:

- Any email and password sign in a demo user; sign-up and password reset say they are off.
- The settings work until the app closes, and deleting the account signs out.
- Other calls answer as an API with nothing set up would, and routes the demo leaves out answer 404, as for a module the project doesn't have.
- A banner says it is a demo, with sample data that stays on the device.

Builds without `--demo` contain none of it. The demo replaces the app's build in the native project, so run `pnpm --filter @clubedge/shell sync` before building a release.

Each Starter release from v0.27.0 attaches the Starter's own demo for Android, [clubedge-demo.apk](https://github.com/Clubedge/clubedge-starter/releases/latest/download/clubedge-demo.apk). It is built from a project with React + Vite, Hono, Better Auth, file storage, and background jobs, whose sample job runs on the phone.

## CI [#ci]

The project's CI builds a debug APK on each pull request and push to `main`, and keeps it for 7 days as the run's **android-debug** artifact, to install on a device. Set a `WEB_URL` repository variable so that it calls your website; without one, it calls `https://example.com`.

## Limits [#limits]

- Only the React + Vite frontend can be packaged, with either API.
- The app needs the deployed website: it calls the API through it, and emailed links open it. The app runs no server, so [`pnpm clubedge deploy`](../cli/deploy/index.mdx) deploys the website and the API and leaves it out.
- No in-app payments: subscriptions stay on the website.
- The stores' accounts, listings, and review are yours. The builds and their signing are set up: see [Store releases](#store-releases).

## When the app can't reach the API [#when-the-app-cant-reach-the-api]

Check that `WEB_URL` in `apps/shell/.env.local` is the deployed website's address, and that the website's `MOBILE_ORIGINS` lists the app's origins. Then run `pnpm --filter @clubedge/shell sync` again.
