fix(client): switch bridge token to import.meta.env (Vite auto-expose) — Patch 025

Patch 023 attempted to read process.env.VITE_BRIDGE_TOKEN, but the
vite.config.ts define block uses { 'process.env': {...} } which only
substitutes the literal expression 'process.env' standalone, not when
followed by a member access like 'process.env.X'. So the runtime call
evaluated to undefined and the bridge interceptor never sent a Bearer
header — every /database call got 401.

Switch to Vite's standard pattern: VITE_BRIDGE_TOKEN lives in
apps/client/.env.local (gitignored, must be created locally) and is
auto-exposed via import.meta.env. Verified: the dev server substitutes
it inline at transform time as 'brg_smoketest_admin'.

Patch 025.
This commit is contained in:
Corentin JOGUET 2026-05-08 12:44:18 +02:00
parent e027ae9357
commit 9b33a2683b

View file

@ -56,16 +56,17 @@ export function createBridgeClient(bridgeUrl: string): AxiosInstance {
timeout: 15_000, timeout: 15_000,
}); });
// Vite auto-exposes VITE_* vars from apps/client/.env(.local) via
// import.meta.env. The monorepo-root .env is not picked up automatically
// for client-side consumption, so the dev token lives in apps/client/.env.local
// (gitignored).
const envToken: string | undefined = (
import.meta as unknown as { env?: { VITE_BRIDGE_TOKEN?: string } }
).env?.VITE_BRIDGE_TOKEN;
instance.interceptors.request.use((config) => { instance.interceptors.request.use((config) => {
// Priority: cookie token (prod) > VITE_BRIDGE_TOKEN env (dev fallback). // Priority: cookie token (prod) > VITE_BRIDGE_TOKEN env (dev fallback)
// Vite's define block in vite.config.ts injects VITE_BRIDGE_TOKEN into
// process.env at build/dev time (not into import.meta.env, since the .env
// is loaded from the monorepo root, not from apps/client/).
const cookieToken = readTokenFromCookie(); const cookieToken = readTokenFromCookie();
const envToken =
typeof process !== "undefined"
? (process.env as unknown as { VITE_BRIDGE_TOKEN?: string })?.VITE_BRIDGE_TOKEN
: undefined;
const token = cookieToken || envToken; const token = cookieToken || envToken;
if (token) { if (token) {
config.headers["Authorization"] = `Bearer ${token}`; config.headers["Authorization"] = `Bearer ${token}`;