From dae347968086b67b656f93f9572ed5a99113f9a4 Mon Sep 17 00:00:00 2001 From: Pc Date: Sun, 4 Jan 2026 13:43:04 +0100 Subject: [PATCH] fix: not adding subdomain address to fetch api --- .../src/context/AuthProvider.tsx | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/kittyurl-frontend/src/context/AuthProvider.tsx b/kittyurl-frontend/src/context/AuthProvider.tsx index b6a0204..87d4abf 100644 --- a/kittyurl-frontend/src/context/AuthProvider.tsx +++ b/kittyurl-frontend/src/context/AuthProvider.tsx @@ -3,9 +3,6 @@ import { AuthContext } from './AuthContext'; import { sha512 } from '../utils/crypto'; import type { AuthResponse } from '../types/auth'; -/** - * Funkcja pomocnicza do pobierania subdomeny. - */ const getSubdomain = (): string | null => { const hostname = window.location.hostname; const parts = hostname.split('.'); @@ -28,22 +25,21 @@ export function AuthProvider({ children }: { children: ReactNode }) { try { const hashedPassword = await sha512(pass); - // Korzystamy z relatywnej ścieżki /api, którą Vite Proxy przekaże dalej. - // Usuwamy API_BASE_URL, aby uniknąć błędów o nieużywanej zmiennej. - const pathPrefix = subdomain ? `/api/${subdomain}` : '/api'; - const fullUrl = `${pathPrefix}/v1/user/${endpoint}`; + // POPRAWKA: Ścieżka URL jest stała, zgodna z Twoim API. + // Vite Proxy przechwyci '/api' i skieruje to na https://www.ktty.is + const fullUrl = `/api/v1/user/${endpoint}`; const response = await fetch(fullUrl, { method: 'POST', headers: { 'accept': 'application/json', 'Content-Type': 'application/json', + // Subdomenę przekazujemy w nagłówku, aby backend wiedział, który to klient 'X-Subdomain': subdomain || '', }, body: JSON.stringify({ name, password: hashedPassword, ttl: 86400 }), }); - // Rozwiązanie błędu "Unexpected any": definiujemy typ danych przed użyciem const data: AuthResponse = await response.json(); if (!response.ok) { @@ -57,15 +53,8 @@ export function AuthProvider({ children }: { children: ReactNode }) { } return data; } catch (err: unknown) { - // Rozwiązanie błędu "Unexpected any" w bloku catch: let errorMessage = 'Wystąpił nieoczekiwany błąd'; - - if (err instanceof Error) { - errorMessage = err.message; - } else if (typeof err === 'string') { - errorMessage = err; - } - + if (err instanceof Error) errorMessage = err.message; setError(errorMessage); return null; } finally {