fix: vite config allowedhost read from .env
All checks were successful
Update changelog / changelog (push) Successful in 26s

This commit is contained in:
Pc
2026-01-03 15:54:50 +01:00
parent ceeb2cccaf
commit 95f449a3d2
4 changed files with 46 additions and 67 deletions

View File

@@ -1,20 +1,35 @@
// src/context/AuthProvider.tsx
import { useState, useCallback, type ReactNode } from 'react';
import { AuthContext } from './AuthContext'; // Importujemy stałą z pliku obok
import { useState, useCallback, type ReactNode } from 'react';
import { AuthContext } from './AuthContext';
import { sha512 } from '../utils/crypto';
import type { AuthResponse } from '../types/auth';
// Pobieramy bazowy adres i usuwamy ewentualny ukośnik na końcu
// Używamy import.meta.env, który jest dostępny wewnątrz kodu React
const API_BASE_URL = (import.meta.env.VITE_API_TARGET || '').replace(/\/$/, '');
export function AuthProvider({ children }: { children: ReactNode }) {
const [token, setToken] = useState<string | null>(sessionStorage.getItem('ktty_token'));
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
const authRequest = useCallback(async (endpoint: 'signUp' | 'signIn', name: string, pass: string) => {
if (!API_BASE_URL) {
const msg = "Błąd konfiguracji: VITE_API_TARGET jest pusty. Sprawdź plik .env i restartuj serwer.";
setError(msg);
return null;
}
setLoading(true);
setError(null);
try {
const hashedPassword = await sha512(pass);
const response = await fetch(`/api/v1/user/${endpoint}`, {
const fullUrl = `${API_BASE_URL}/api/v1/user/${endpoint}`;
const response = await fetch(fullUrl, {
method: 'POST',
headers: {
'accept': 'application/json',
@@ -23,16 +38,26 @@ export function AuthProvider({ children }: { children: ReactNode }) {
body: JSON.stringify({ name, password: hashedPassword, ttl: 86400 }),
});
const data: AuthResponse = await response.json();
if (!response.ok) throw new Error(data.error || data.message || `Error ${response.status}`);
const contentType = response.headers.get("content-type");
let data: AuthResponse | null = null;
if (data.token) {
if (contentType && contentType.includes("application/json")) {
data = await response.json();
}
if (!response.ok) {
const errorMsg = data?.error || data?.message || `Błąd serwera: ${response.status}`;
throw new Error(errorMsg);
}
if (data && data.token) {
sessionStorage.setItem('ktty_token', data.token);
setToken(data.token); // To aktualizuje stan w całej aplikacji natychmiast!
setToken(data.token);
}
return data;
} catch (err: unknown) {
setError(err instanceof Error ? err.message : 'Unknown error');
const message = err instanceof Error ? err.message : 'Unknown error';
setError(message);
return null;
} finally {
setLoading(false);