This commit is contained in:
@@ -1,11 +1,24 @@
|
||||
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 { sha512 } from '../utils/crypto';
|
||||
import type { AuthResponse } from '../types/auth';
|
||||
|
||||
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 hostname = window.location.hostname;
|
||||
@@ -15,10 +28,11 @@ const getSubdomain = (): string | null => {
|
||||
};
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const config = getCookieConfig();
|
||||
const subdomain = getSubdomain();
|
||||
|
||||
// Pobieramy token z ciasteczka zamiast z sessionStorage
|
||||
const [token, setToken] = useState<string | null>(Cookies.get(TOKEN_KEY) || null);
|
||||
// Inicjalizacja stanu bezpośrednio z ciasteczka
|
||||
const [token, setToken] = useState<string | null>(() => Cookies.get(TOKEN_KEY) || null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
@@ -47,11 +61,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
|
||||
if (data?.token) {
|
||||
// ZAPISYWANIE CIASTECZKA DLA WSZYSTKICH SUBDOMEN
|
||||
// 2. ZAPIS Z DYNAMICZNĄ KONFIGURACJĄ
|
||||
Cookies.set(TOKEN_KEY, data.token, {
|
||||
domain: PARENT_DOMAIN,
|
||||
expires: 1, // 1 dzień
|
||||
secure: true, // Wymagane dla HTTPS
|
||||
domain: config.domain,
|
||||
expires: 7, // wydłużamy do 7 dni dla wygody
|
||||
secure: config.secure,
|
||||
sameSite: 'lax'
|
||||
});
|
||||
setToken(data.token);
|
||||
@@ -65,13 +79,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, [subdomain]);
|
||||
}, [subdomain, config]);
|
||||
|
||||
const logout = useCallback(() => {
|
||||
// Usuwamy ciasteczko z domeny nadrzędnej
|
||||
Cookies.remove(TOKEN_KEY, { domain: PARENT_DOMAIN });
|
||||
// 3. USUNIĘCIE MUSI MIEĆ TĘ SAMĄ DOMENĘ CO ZAPIS
|
||||
Cookies.remove(TOKEN_KEY, { domain: config.domain });
|
||||
setToken(null);
|
||||
}, []);
|
||||
}, [config]);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{
|
||||
|
||||
Reference in New Issue
Block a user