From 0cbf5c7021edd5bba1ca093eec02ff3c55b2576b Mon Sep 17 00:00:00 2001 From: gitgrahamdunn Date: Wed, 18 Feb 2026 16:04:21 -0700 Subject: [PATCH] Fix frontend auth API base URL for Vercel --- frontend/.env.example | 4 ++-- frontend/README.md | 21 +++++++++++++++++++++ frontend/src/api.ts | 16 +++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/frontend/.env.example b/frontend/.env.example index 15f3710..35270f9 100644 --- a/frontend/.env.example +++ b/frontend/.env.example @@ -1,2 +1,2 @@ -# Frontend API base URL (single source of truth used by src/api.ts) -VITE_API_BASE_URL=http://127.0.0.1:8000 +# Backend API origin used by frontend requests (no trailing slash) +VITE_API_URL=http://127.0.0.1:8000 diff --git a/frontend/README.md b/frontend/README.md index 5dce044..d1cea78 100644 --- a/frontend/README.md +++ b/frontend/README.md @@ -10,6 +10,27 @@ React + Vite frontend for the EDMS backend. - Mark READY / Abandon / Merge to Plant from project detail - Document search (`/documents` and `/documents/search`) +## Environment configuration + +Set `VITE_API_URL` to your backend origin (no trailing slash). The frontend now reads this value for all API requests. + +```bash +cd frontend +cp .env.example .env +# local backend example +VITE_API_URL=http://127.0.0.1:8000 +``` + +If `VITE_API_URL` is missing, the app logs a clear `console.error` and falls back to same-origin requests. + +### Vercel setup + +In the Vercel frontend project, add: + +- `VITE_API_URL=https://gitplant-backend.vercel.app` + +Then redeploy so auth/login requests target the deployed backend. + ## Run locally ```bash cd frontend diff --git a/frontend/src/api.ts b/frontend/src/api.ts index bf2c3df..425a614 100644 --- a/frontend/src/api.ts +++ b/frontend/src/api.ts @@ -17,9 +17,19 @@ import type { WorkingRevisionStatusResponse, } from "./types"; -const API_BASE_URL = - (import.meta as ImportMeta & { env?: Record }).env - ?.VITE_API_BASE_URL ?? "http://localhost:8000"; +const frontendEnv = (import.meta as ImportMeta & { + env?: Record; +}).env; + +const configuredApiUrl = frontendEnv?.VITE_API_URL?.trim(); +const API_BASE_URL = configuredApiUrl || window.location.origin; + +if (!configuredApiUrl) { + console.error( + "[api] Missing required VITE_API_URL. Falling back to same-origin requests. " + + "Set VITE_API_URL to your deployed backend URL (for example, https://gitplant-backend.vercel.app).", + ); +} function getAuthHeaders(token: string): HeadersInit { return { Authorization: `Bearer ${token}` };