fix(client): read VITE_BRIDGE_TOKEN via process.env (define block target) — Patch 023

The previous fix (Patch 021) read import.meta.env.VITE_BRIDGE_TOKEN, but
Vite only auto-exposes VITE_* in import.meta.env when the .env lives in
apps/client/. Our .env is at the monorepo root and is loaded via
loadEnv() in vite.config.ts, then injected through the define block
under the 'process.env' key. So the runtime variable lives at
process.env.VITE_BRIDGE_TOKEN, not import.meta.env.VITE_BRIDGE_TOKEN.

Without this, the bridge proxy received no Bearer header and returned
401 Unauthorized for every /database slash command request — exactly
what Corentin reported.

Patch 023.
This commit is contained in:
Corentin JOGUET 2026-05-08 12:34:38 +02:00
parent aef912f9a4
commit 5f7bce9b02

View file

@ -57,10 +57,15 @@ export function createBridgeClient(bridgeUrl: string): AxiosInstance {
}); });
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 metaEnv = (import.meta as unknown as { env?: { VITE_BRIDGE_TOKEN?: string } }).env; const envToken =
const envToken = metaEnv?.VITE_BRIDGE_TOKEN; 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}`;