feat(waiter): native pairing, venue switcher, QR scanner, Android back handling

- First run in the native app shows only the pairing screen (VenuesPage):
  scan the manager's pairing QR or type the server address; the server must
  answer /api/system/identity as xenia-pos with a supported api_version, and
  a QR's ?pair=<site_id> must match - a code from another venue is refused
- /venues (native only): list of paired venues, switch (reloads into that
  venue's own token/IndexedDB), remove (data kept - may hold unsynced
  orders). Reachable from the login screen, the user menu and the offline page
- QrScannerModal: WebView camera via qr-scanner (no Google Play Services
  dependency, carries over to iOS); requests the camera once up front so the
  Android prompt appears a single time and "denied" gets its own message
- Hardware back: @capacitor/app listener - no-op on root screens (/, /tables,
  /login), otherwise history back. Path-based because Chrome's history
  intervention makes the WebView report canGoBack=false for the sentinel
  entry AndroidBackGuard pushes
- Before pairing no IndexedDB is opened, so nothing is created under the
  un-namespaced name
- InstallAppBanner now shows in plain-HTTP browser mode only when the venue
  server actually serves the APK (HEAD /downloads/xenia-waiter.apk)

Verified on an Android 15 emulator (API 35) with real taps via adb and state
via WebView DevTools: wrong address -> error; other venue's QR -> refused;
pairing -> venue-namespaced storage, live WebSocket; back on /tables keeps the
app open; restart keeps venue + session; second venue pairs logged-out and
switching back keeps venue 1's session; camera deny -> one prompt + message;
allow -> live preview. Signed release APK installed and paired. Web modes
(same-origin, plain-HTTP LAN IP, VITE_SERVER_URL) re-verified in Edge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-28 13:31:23 +03:00
co-authored by Claude Opus 5.5
parent 0b0f5c60c3
commit a486e3ff63
10 changed files with 440 additions and 12 deletions
+104
View File
@@ -0,0 +1,104 @@
/**
* Pairing a phone with a venue server (native app).
*
* Input comes from the pairing QR code shown in the manager
* (`http://<LAN IP>/?pair=<site_id>`) or from an address typed by hand
* (`192.168.1.50`, `192.168.1.50:8080`, `http://…`). Either way the server must
* answer GET /api/system/identity as a Xenia server with an API version this app
* understands, and — when the QR carried a site id — the SAME site id, so a code
* from one venue can't silently pair with a different machine that took its IP.
*
* Contract: docs/reference/lan-access.md
*/
import { saveVenue } from '../config/server'
// api_version values (from /api/system/identity) this build can talk to.
export const SUPPORTED_API_VERSIONS = [1]
const PROBE_TIMEOUT_MS = 5000
export class PairingError extends Error {}
/**
* Turn scanned or typed text into { baseUrl, expectedSiteId }.
* Only the origin of a URL is kept — paths and other params are ignored.
*/
export function parseServerInput(raw) {
const text = String(raw || '').trim()
if (!text) throw new PairingError('Συμπληρώστε τη διεύθυνση του server.')
const withScheme = /^https?:\/\//i.test(text) ? text : `http://${text}`
let url
try {
url = new URL(withScheme)
} catch {
throw new PairingError('Μη έγκυρη διεύθυνση. Παράδειγμα: 192.168.1.50')
}
if (!url.hostname) throw new PairingError('Μη έγκυρη διεύθυνση. Παράδειγμα: 192.168.1.50')
return {
baseUrl: url.origin,
expectedSiteId: url.searchParams.get('pair') || null,
}
}
/** Ask a server who it is. Resolves to the identity object or throws PairingError. */
export async function probeServer(baseUrl) {
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), PROBE_TIMEOUT_MS)
let res
try {
res = await fetch(`${baseUrl}/api/system/identity`, { signal: controller.signal })
} catch {
throw new PairingError(
'Δεν βρέθηκε server σε αυτή τη διεύθυνση. Ελέγξτε ότι το κινητό είναι στο ίδιο WiFi με το κατάστημα.'
)
} finally {
clearTimeout(timer)
}
if (res.status === 403) {
throw new PairingError('Ο server αρνήθηκε τη σύνδεση. Το κινητό πρέπει να είναι στο WiFi του καταστήματος.')
}
let identity = null
try {
identity = await res.json()
} catch {
// not JSON → not a Xenia server
}
if (!res.ok || identity?.app !== 'xenia-pos') {
throw new PairingError('Η διεύθυνση δεν ανήκει σε server Xenia.')
}
if (!SUPPORTED_API_VERSIONS.includes(identity.api_version)) {
throw new PairingError(
`Ο server (έκδοση ${identity.version}) δεν είναι συμβατός με αυτή την εφαρμογή. Ενημερώστε την εφαρμογή ή τον server.`
)
}
return identity
}
/** Storage-safe venue id for servers without a SITE_ID (dev / unregistered). */
function fallbackSiteId(baseUrl) {
return 'local-' + new URL(baseUrl).host.replace(/[^a-z0-9]+/gi, '-')
}
/**
* Validate a scanned/typed address and save it as a paired venue.
* Does NOT switch to it — the caller decides (switchVenue reloads the app).
*/
export async function pairWith(raw) {
const { baseUrl, expectedSiteId } = parseServerInput(raw)
const identity = await probeServer(baseUrl)
if (expectedSiteId && identity.site_id && identity.site_id !== expectedSiteId) {
throw new PairingError(
'Σε αυτή τη διεύθυνση απαντά διαφορετικό κατάστημα από αυτό του QR. Ζητήστε νέο QR από τον διαχειριστή.'
)
}
return saveVenue({
siteId: identity.site_id || fallbackSiteId(baseUrl),
name: identity.venue_name || new URL(baseUrl).host,
baseUrl,
})
}