fix: subdomain fix
All checks were successful
Update changelog / changelog (push) Successful in 31s

This commit is contained in:
Pc
2026-01-04 14:07:16 +01:00
parent c765c92f89
commit 3c1d66ba48

View File

@@ -1,11 +1,24 @@
import { useState, useCallback, type ReactNode } from 'react'; import { useState, useCallback, type ReactNode } from 'react';
import Cookies from 'js-cookie'; // Importujemy bibliotekę do ciasteczek import Cookies from 'js-cookie';
import { AuthContext } from './AuthContext'; 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';
const TOKEN_KEY = 'ktty_shared_token'; const TOKEN_KEY = 'ktty_shared_token';
const PARENT_DOMAIN = '.ktty.is'; // Kropka na początku pozwala na dostęp ze wszystkich subdomen
// 1. DYNAMICZNE USTALANIE DOMENY
const getCookieConfig = () => {
const hostname = window.location.hostname;
// Jeśli testujesz na localhost, nie ustawiamy atrybutu 'domain'
if (hostname === 'localhost' || hostname === '127.0.0.1') {
return { domain: undefined, secure: false };
}
// Na produkcji używamy domeny nadrzędnej z kropką
// To pozwoli s.ktty.is i ktty.is widzieć to samo ciasteczko
return { domain: '.ktty.is', secure: true };
};
const getSubdomain = (): string | null => { const getSubdomain = (): string | null => {
const hostname = window.location.hostname; const hostname = window.location.hostname;
@@ -15,10 +28,11 @@ const getSubdomain = (): string | null => {
}; };
export function AuthProvider({ children }: { children: ReactNode }) { export function AuthProvider({ children }: { children: ReactNode }) {
const config = getCookieConfig();
const subdomain = getSubdomain(); const subdomain = getSubdomain();
// Pobieramy token z ciasteczka zamiast z sessionStorage // Inicjalizacja stanu bezpośrednio z ciasteczka
const [token, setToken] = useState<string | null>(Cookies.get(TOKEN_KEY) || null); const [token, setToken] = useState<string | null>(() => Cookies.get(TOKEN_KEY) || null);
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null); const [error, setError] = useState<string | null>(null);
@@ -47,11 +61,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
} }
if (data?.token) { if (data?.token) {
// ZAPISYWANIE CIASTECZKA DLA WSZYSTKICH SUBDOMEN // 2. ZAPIS Z DYNAMICZNĄ KONFIGURACJĄ
Cookies.set(TOKEN_KEY, data.token, { Cookies.set(TOKEN_KEY, data.token, {
domain: PARENT_DOMAIN, domain: config.domain,
expires: 1, // 1 dzień expires: 7, // wydłużamy do 7 dni dla wygody
secure: true, // Wymagane dla HTTPS secure: config.secure,
sameSite: 'lax' sameSite: 'lax'
}); });
setToken(data.token); setToken(data.token);
@@ -65,13 +79,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
} finally { } finally {
setLoading(false); setLoading(false);
} }
}, [subdomain]); }, [subdomain, config]);
const logout = useCallback(() => { const logout = useCallback(() => {
// Usuwamy ciasteczko z domeny nadrzędnej // 3. USUNIĘCIE MUSI MIEĆ TĘ SAMĄ DOMENĘ CO ZAPIS
Cookies.remove(TOKEN_KEY, { domain: PARENT_DOMAIN }); Cookies.remove(TOKEN_KEY, { domain: config.domain });
setToken(null); setToken(null);
}, []); }, [config]);
return ( return (
<AuthContext.Provider value={{ <AuthContext.Provider value={{