import axios from 'axios' import { createApiClient } from '@xenia/api' import { deviceStore } from '../device/storage' // The shared client (@xenia/api, CS-18). Same origin: the image's nginx (or Vite in dev) // forwards /api to the backend. Every request carries the screen's device token // (X-Xenia-Device) and, when someone is signed in, their session token. // A 401 = the session ended (signed out elsewhere, idle, device reassigned): drop the session // token; the screen goes back to its sign-in (3.1c). Pairing/sign-in forms pass // { xenia: { ignoreUnauthorized: true } } for their own 401s. const listeners = new Set() const client = createApiClient({ axios, baseURL: '', getToken: () => deviceStore.sessionToken(), getDeviceToken: () => deviceStore.deviceToken(), onUnauthorized: x => { deviceStore.setSessionToken(null) listeners.forEach(fn => fn(x)) }, }) /** Called with err.xenia whenever a request comes back 401 (see above). */ export function onUnauthorized(fn) { listeners.add(fn) return () => listeners.delete(fn) } export default client