From 1377a973986b008060e7c77716f6c1d3809fc852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20S=C3=B6derberg?= Date: Sun, 22 Feb 2026 21:33:09 +0100 Subject: [PATCH] Updated API for production --- frontend/src/services/api.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/frontend/src/services/api.js b/frontend/src/services/api.js index 97aeaae..1afb66c 100644 --- a/frontend/src/services/api.js +++ b/frontend/src/services/api.js @@ -2,14 +2,11 @@ const getBackendHost = () => { const host = window.location.hostname || 'localhost'; - return host; + return window.location.protocol === 'https:' ? host : `${host}:8000`; }; -const port = '8000'; - -export const API_BASE = `${window.location.protocol}//${getBackendHost()}:${port}/api`; -export const WS_URL = `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${getBackendHost()}:${port}/api/ws`; - +export const API_BASE = `${window.location.protocol}//${getBackendHost()}/api`; +export const WS_URL = `${window.location.protocol === 'https:' ? 'wss:' : 'ws:'}//${getBackendHost()}/api/ws`; export const getToken = () => localStorage.getItem('volleyToken'); @@ -24,16 +21,19 @@ const api = { if (data) opts.body = isFormData ? data : JSON.stringify(data); // Ensure clean URL concatenation - const baseUrl = API_BASE.endsWith('/') ? API_BASE.slice(0, -1) : API_BASE; + const baseUrl = API_BASE.replace(/\/$/, ''); const endpoint = url.startsWith('/') ? url : `/${url}`; const res = await fetch(`${baseUrl}${endpoint}`, opts); if (!res.ok) { if (res.status === 401) { localStorage.removeItem('volleyToken'); - window.location.href = '/login'; + if (window.location.pathname !== '/login') { + window.location.href = '/login'; + } } - throw await res.json(); + const errorData = await res.json().catch(() => ({ detail: 'An error occurred' })); + throw errorData; } return res.json(); },