fix: not adding subdomain address to fetch api
All checks were successful
Update changelog / changelog (push) Successful in 28s

This commit is contained in:
Pc
2026-01-04 13:43:04 +01:00
parent 549da339e4
commit dae3479680

View File

@@ -3,9 +3,6 @@ import { AuthContext } from './AuthContext';
import { sha512 } from '../utils/crypto'; import { sha512 } from '../utils/crypto';
import type { AuthResponse } from '../types/auth'; import type { AuthResponse } from '../types/auth';
/**
* Funkcja pomocnicza do pobierania subdomeny.
*/
const getSubdomain = (): string | null => { const getSubdomain = (): string | null => {
const hostname = window.location.hostname; const hostname = window.location.hostname;
const parts = hostname.split('.'); const parts = hostname.split('.');
@@ -28,22 +25,21 @@ export function AuthProvider({ children }: { children: ReactNode }) {
try { try {
const hashedPassword = await sha512(pass); const hashedPassword = await sha512(pass);
// Korzystamy z relatywnej ścieżki /api, którą Vite Proxy przekaże dalej. // POPRAWKA: Ścieżka URL jest stała, zgodna z Twoim API.
// Usuwamy API_BASE_URL, aby uniknąć błędów o nieużywanej zmiennej. // Vite Proxy przechwyci '/api' i skieruje to na https://www.ktty.is
const pathPrefix = subdomain ? `/api/${subdomain}` : '/api'; const fullUrl = `/api/v1/user/${endpoint}`;
const fullUrl = `${pathPrefix}/v1/user/${endpoint}`;
const response = await fetch(fullUrl, { const response = await fetch(fullUrl, {
method: 'POST', method: 'POST',
headers: { headers: {
'accept': 'application/json', 'accept': 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
// Subdomenę przekazujemy w nagłówku, aby backend wiedział, który to klient
'X-Subdomain': subdomain || '', 'X-Subdomain': subdomain || '',
}, },
body: JSON.stringify({ name, password: hashedPassword, ttl: 86400 }), 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(); const data: AuthResponse = await response.json();
if (!response.ok) { if (!response.ok) {
@@ -57,15 +53,8 @@ export function AuthProvider({ children }: { children: ReactNode }) {
} }
return data; return data;
} catch (err: unknown) { } catch (err: unknown) {
// Rozwiązanie błędu "Unexpected any" w bloku catch:
let errorMessage = 'Wystąpił nieoczekiwany błąd'; let errorMessage = 'Wystąpił nieoczekiwany błąd';
if (err instanceof Error) errorMessage = err.message;
if (err instanceof Error) {
errorMessage = err.message;
} else if (typeof err === 'string') {
errorMessage = err;
}
setError(errorMessage); setError(errorMessage);
return null; return null;
} finally { } finally {