batchCooking/apps/web/src/api/capacitor-token-store.ts
Nicolas ff36cefcde
Some checks failed
CI / lint (push) Failing after 1m59s
CI / build (push) Successful in 3m55s
CI / test (push) Has been cancelled
CI / intent-service-test (push) Has been cancelled
CI / e2e (push) Has been cancelled
feat(web): plomberie web pour la coquille Capacitor Android (#39 phase 2)
Tout ce qui est vérifiable en CI de la phase 2 du portage mobile. La
génération du projet natif (`npx cap add android`), le build de l'APK et
le test sur appareil restent une étape manuelle (pas d'Android Studio ici)
— cf. le corps de l'issue #39.

- Dépendances : `@capacitor/core`, `@capacitor/android`,
  `@capacitor/preferences` (+ `@capacitor/cli` en dev), toutes en 8.5.x.
- `capacitor.config.ts` : `appId` `fr.kyuno.batchcooking`, `webDir: "dist"`.
- `apps/web/src/api/capacitor-token-store.ts` : implémentation `TokenStore`
  adossée à `@capacitor/preferences`, branchée via `setTokenStore()` (le
  point d'injection posé en phase 1). Importée dynamiquement dans
  `main.tsx` uniquement quand `Capacitor.isNativePlatform()` — le bundle
  web ne charge jamais `@capacitor/preferences` (vérifié : chunk séparé de
  0,5 ko jamais requis côté navigateur).
- `main.tsx` : `HashRouter` en natif (le bundle packagé sert `dist/`
  depuis l'origine locale de la WebView, sans réécriture des liens
  profonds vers `index.html`), `BrowserRouter` inchangé sur le web ;
  bootstrap async pour poser le token store avant le premier rendu.
- `build:capacitor` (`vite build --mode capacitor`) + `.env.capacitor.example` :
  le build packagé a besoin de l'URL absolue de l'API en dur (pas de
  "même origine que la page" possible).

Vérifié : `pnpm build` + `pnpm lint` verts ; Cypress smoke/layout/sidebar
14/14 en local (le refactor du bootstrap et le choix de routeur ne
cassent rien côté web).

Refs #39
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-06 03:56:15 +02:00

30 lines
1.1 KiB
TypeScript

import { Preferences } from "@capacitor/preferences";
import type { TokenStore } from "./token-store";
/** Key the session JWT is filed under in `@capacitor/preferences`. */
const TOKEN_KEY = "auth_token";
/**
* The native {@link TokenStore}: the session JWT persisted through
* `@capacitor/preferences` (Android `SharedPreferences` under the hood),
* so it survives app restarts the way the browser's httpOnly cookie
* survives a page reload.
*
* Only ever installed (via `setTokenStore`, see `client.ts`) when
* `Capacitor.isNativePlatform()` — the plain web build keeps the no-op
* store and its cookie, and this module is never imported there (the
* bootstrap in `main.tsx` `import()`s it lazily behind that check), so
* `@capacitor/preferences` stays out of the web bundle.
*/
export const capacitorTokenStore: TokenStore = {
async get() {
const { value } = await Preferences.get({ key: TOKEN_KEY });
return value;
},
async set(token) {
await Preferences.set({ key: TOKEN_KEY, value: token });
},
async clear() {
await Preferences.remove({ key: TOKEN_KEY });
},
};