Send the auth token on every API call; build against the API's own domain

Seven call sites used fetch() directly instead of the api client in
src/api/client.js, so they sent no Authorization header. Four of them hit
endpoints the backend guards with a permission, and every one of those returned
401 for any signed-in user:

  AdminPage  /api/admin/training/upload-dataset      (upload_train_test)
  AdminPage  /api/admin/training/allocate-discounts  (allocate_discounts)
  UserPage   /api/user/products/add                  (add_product)
  UserPage   /api/user/products/upload-file          (upload_batch_products)

The remaining three hit public GETs and worked, but bypassed VITE_API_BASE_URL
and the central 401 handler just the same.

Route all seven through the client and add the endpoints it was missing.
Multipart uploads still need a raw fetch, because the browser has to set its
own Content-Type to carry the boundary, so that is now one upload() helper that
attaches the auth header and routes 401s to the logout handler rather than
three copies that did neither.

handleAllocateDiscounts only acted on res.ok, so a 401 or 403 left the panel
looking idle with no indication that nothing had been allocated. It surfaces
the error now.

Take VITE_API_BASE_URL as a Docker build arg. The app is served from
catalogue.nearle.ai.in and the API from mcp.catalogue.nearle.ai.in, and Vite
inlines env vars at build time, so setting this on the running container does
nothing - it has to reach npm run build. Defaults to empty, which keeps
requests relative for the same-origin nginx proxy.

Also revoke the object URL after the sample-CSV download.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Suriyakumarvijayanayagam
2026-08-13 12:31:06 +05:30
parent dae0b3e0fb
commit ea677c72df
5 changed files with 149 additions and 123 deletions

View File

@@ -6,9 +6,22 @@ COPY package.json package-lock.json ./
RUN npm ci
COPY . .
# VITE_API_BASE_URL is intentionally left unset: nginx.conf proxies /api/*
# to the backend container, so the built app can keep using relative
# same-origin requests (see src/api/client.js).
# Which origin the built app sends its API calls to.
#
# Vite inlines env vars at build time, so this MUST be a build arg - setting it
# on the running container has no effect, the value is already baked into the
# JS bundle.
#
# docker build --build-arg VITE_API_BASE_URL=https://mcp.catalogue.nearle.ai.in .
#
# Left empty by default, which keeps requests relative and same-origin so the
# nginx /api/* proxy below serves them. Set it when the API is on its own
# domain - and add this app's origin to the backend's API_CORS_ORIGINS, or the
# browser will block the responses.
ARG VITE_API_BASE_URL=""
ENV VITE_API_BASE_URL=$VITE_API_BASE_URL
RUN npm run build
# ---- Serve stage ----