Files
Trackeep/frontend/src/lib/credentials.ts
T
Tomas Dvorak 4dfdd500b4
CI/CD Pipeline / Test (push) Successful in 20m59s
CI/CD Pipeline / Security Scan (push) Successful in 10m38s
CI/CD Pipeline / Build and Push Images (push) Failing after 13s
fix(frontend): resolve production API URL fallback to localhost
Problem:
The unified Docker image builds the frontend at build time without
VITE_API_URL. Vite inlined import.meta.env.VITE_API_URL as undefined,
so every API call fell back to the hardcoded 'http://localhost:8080'.
This broke Casa deployments where the frontend loaded from the public
 domain but tried to reach the backend at localhost.

Solution:
1. Centralize API URL resolution in lib/api-url.ts via getApiOrigin().
   It checks runtime window.ENV first (injected by docker-entrypoint.sh
   at container startup), then build-time import.meta.env, then dev
   fallback. In production unified deployments it returns '' so API
   calls use same-origin relative URLs (/api/v1/...) that nginx proxies
   to the backend.
2. Replace all 50+ inline import.meta.env.VITE_API_URL || 'localhost'
   usages across 14 source files with getApiOrigin() / getApiV1BaseUrl().
3. Add build args and runtime sed substitution to Dockerfile and
   docker-entrypoint.sh so the same image works for any deployment.
4. Pass VITE_API_URL through docker-compose.yml and CI/CD build-args.

Verified:
- Production bundle contains 0 occurrences of localhost:8080.
- Container health check and /api/v1/auth/check-users both return 200.
- Runtime injection correctly sets VITE_API_URL='' for same-origin
  and VITE_API_URL='https://domain' for external backend.

Generated with [Devin](https://cli.devin.ai/docs)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-05-22 12:34:39 +02:00

77 lines
2.7 KiB
TypeScript

// Utility functions to check if credentials are configured in environment variables
// Check if database credentials are configured
export const hasDatabaseCredentials = (): boolean => {
return !!(import.meta.env.VITE_DB_HOST &&
import.meta.env.VITE_DB_USER &&
import.meta.env.VITE_DB_PASSWORD &&
import.meta.env.VITE_DB_NAME) ||
!!(import.meta.env.DB_HOST &&
import.meta.env.DB_USER &&
import.meta.env.DB_PASSWORD &&
import.meta.env.DB_NAME);
};
// Check if search API credentials are configured
export const hasSearchCredentials = (): boolean => {
return !!(import.meta.env.VITE_BRAVE_API_KEY ||
import.meta.env.VITE_SERPER_API_KEY ||
import.meta.env.VITE_SEARCH_API_PROVIDER) ||
!!(import.meta.env.BRAVE_API_KEY ||
import.meta.env.SERPER_API_KEY ||
import.meta.env.SEARCH_API_PROVIDER);
};
// Check if AI service credentials are configured
export const hasAICredentials = (): boolean => {
return !!(import.meta.env.VITE_LONGCAT_API_KEY ||
import.meta.env.VITE_MISTRAL_API_KEY ||
import.meta.env.VITE_GROK_API_KEY ||
import.meta.env.VITE_DEEPSEEK_API_KEY ||
import.meta.env.VITE_OPENROUTER_API_KEY ||
import.meta.env.VITE_OLLAMA_BASE_URL) ||
!!(import.meta.env.LONGCAT_API_KEY ||
import.meta.env.MISTRAL_API_KEY ||
import.meta.env.GROK_API_KEY ||
import.meta.env.DEEPSEEK_API_KEY ||
import.meta.env.OPENROUTER_API_KEY ||
import.meta.env.OLLAMA_BASE_URL);
};
// Check if any credentials are configured
export const hasAnyCredentials = (): boolean => {
return hasDatabaseCredentials() ||
hasSearchCredentials() ||
hasAICredentials();
};
// Check if backend should be available (based on database credentials)
export const isBackendAvailable = (): boolean => {
return hasDatabaseCredentials();
};
// Check if search APIs should be available
export const isSearchAvailable = (): boolean => {
return hasSearchCredentials();
};
// Check if AI services should be available
export const isAIAvailable = (): boolean => {
return hasAICredentials();
};
// Get configured search provider
export const getSearchProvider = (): string => {
return import.meta.env.VITE_SEARCH_API_PROVIDER ||
(import.meta.env.VITE_BRAVE_API_KEY ? 'brave' :
import.meta.env.VITE_SERPER_API_KEY ? 'serper' : 'demo');
};
// Delegates to getApiOrigin so all API URL resolution goes through the
// centralized helper that supports runtime env injection.
import { getApiOrigin } from './api-url';
export const getApiBaseUrl = (): string => {
return getApiOrigin();
};