# © 2026 Evntx FZCO, Dubai, UAE. All rights reserved.

# Reference embedded app — sign-in by one-time code (schema version 2)

A complete, small embedded app to copy from. No package install, no network access needed:
Node 20+ and the files in this directory. The author guide that explains every step is
`docs/customer/embedded-app-author-guide.md`.

| File | Role |
|---|---|
| `manifest.json` | Pack with two apps: `shift-planner` (signs in, `sso.mode: "code"`) and `shift-board` (static, `sso.mode: "none"`) |
| `server.mjs` | The app's server: serves the pages, creates the PKCE pair, redeems the code, keeps its own session |
| `public/aancer-app-sdk.js` | Frame side of the bridge — copy unchanged into your app |
| `public/index.html` + `app.js` | Page of the app that signs in |
| `public/static.html` + `static.js` | Page of the app without sign-in |

## Run it

1. Edit `manifest.json`: put the address your app is served at into `url` and `origin` (https),
   your publisher name and key id. No key yet? `aancer-pack keygen --out <name>.key`
   writes the private key and `<name>.pub`. Sign the pack:
   `aancer-pack sign manifest.json -o shift-planner-2.0.0.ancpack --key <your key>`
   (this pack has no content files).
2. On the platform, as an administrator: Extensions → **Trust keys** → **Add trust key** with your
   public key and the same key id as `publisher.keyId` in the manifest (once per publisher; without
   it the import is refused as not trusted). Then import the pack → **Manage app** on
   *Shift planner* → approve → **Create app secret**. The secret is shown once.
3. Start the server behind your TLS proxy:

```bash
AANCER_PLATFORM_ORIGIN=https://platform.example \
AANCER_APP_SECRET='<the app secret>' \
node server.mjs
```

4. Open *Shift planner* in the platform's Dashboards hub. The page shows **Signed in as <your name>**.

| Variable | Meaning | Default |
|---|---|---|
| `AANCER_PLATFORM_ORIGIN` | Address users open the platform at; the only site allowed to embed the app | required |
| `AANCER_GATEWAY_URL` | Where this server reaches the platform (inside the perimeter) | `AANCER_PLATFORM_ORIGIN` |
| `AANCER_APP_ID` | App id of the manifest | `shift-planner` |
| `AANCER_APP_SECRET` | The app secret | none (the static page works without) |
| `HOST`, `PORT` | Listen address | `127.0.0.1`, `8787` |

Private certificate authority on the platform: `NODE_EXTRA_CA_CERTS=/path/to/ca.pem node server.mjs`.

## What to look at

- The platform token never leaves `server.mjs`. The browser holds a session handle of this app, in
  page memory only.
- `app.js` → `check()`: when the server answers 401, the page asks the platform for a fresh code and
  the server redeems it. Users are never interrupted by an expiry. If the platform cannot be reached
  at that moment, the SDK tries again after 1, 2 and 4 seconds.
- `static.html` works from any plain file host: write the platform address into the `<meta>` tag by
  hand and serve the four files with the `frame-ancestors` header.

## If it does not work

| The page says | Cause | Fix |
|---|---|---|
| Not signed in: This app server has no app secret yet | `AANCER_APP_SECRET` is not set | Create the app secret on the platform and set the variable |
| Not signed in: The app secret on the app server is wrong or was rotated | The secret does not match | Set the current secret and restart |
| Not signed in: The platform could not be reached from the app server | `AANCER_GATEWAY_URL` wrong, or the certificate is not trusted | Check the address; set `NODE_EXTRA_CA_CERTS` |
| Not signed in: The platform did not answer… | `AANCER_PLATFORM_ORIGIN` is not the address in the browser's address bar | Correct it |
| Not signed in: The platform refused the handshake (BRIDGE_PROTOCOL_MISMATCH) | The imported pack declares schema version 1 | Import the version 2 pack |
| Shift board: Rows could not be loaded (…) | The workspace has no dataset with the id `shift_plan`, the capability is not approved, or you may not read the dataset | Create a dataset with that id (or put your dataset's id into `manifest.json` and `static.js`), approve the app, check your access |
| The platform shows "does not allow embedding here" | The app is not reachable from the platform, or a proxy removed the `Content-Security-Policy` header | Let the header through |

Sessions live in this process's memory: run one process, or move the two maps in `server.mjs` to
your shared session store.

The flow is tested as shipped by
`aancer-final-v2/e2e/offline/journeys/embedded-app-code-signin.journey.offline.spec.ts`.
