fix: vite config allowedhost read from .env
All checks were successful
Update changelog / changelog (push) Successful in 26s
All checks were successful
Update changelog / changelog (push) Successful in 26s
This commit is contained in:
10
.env.default
10
.env.default
@@ -1,7 +1,7 @@
|
|||||||
# Site info
|
PUBLIC_URL=https://example.com
|
||||||
PUBLIC_URL=https://example.com # Publicly accessible website root, used for rewrites. Note there is no trailing slash in the URL.
|
USE_SUBDOMAINS=true
|
||||||
USE_SUBDOMAINS=true # Whether backend allows for use of subdomains in URL generation.
|
|
||||||
DEBUG=false
|
DEBUG=false
|
||||||
|
|
||||||
# Frontend specific
|
# Frontend specific
|
||||||
<miejsce na twoje zmienne>
|
VITE_API_TARGET=https://pies.com
|
||||||
VITE_API_TARGET=kitkat.example.com # Target backend for API requests.
|
VITE_ALLOWED_HOST=pies.com
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
import axios from 'axios';
|
|
||||||
import { sha512 } from 'js-sha512';
|
|
||||||
|
|
||||||
// Interfejs zgodny z Twoją dokumentacją [cite: 74-79]
|
|
||||||
export interface AuthResponse {
|
|
||||||
status: "ok";
|
|
||||||
name: string;
|
|
||||||
role: "user" | "admin";
|
|
||||||
token: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const loginUser = async (name: string, pass: string): Promise<AuthResponse> => {
|
|
||||||
// Wysyłamy POST zgodnie ze specyfikacją dokumentacji [cite: 68]
|
|
||||||
const response = await axios.post('https://twoj-backend.pl/api/v1/user/signIn', {
|
|
||||||
name: name,
|
|
||||||
password: sha512(pass) // Wymagane SHA-512 [cite: 71]
|
|
||||||
});
|
|
||||||
|
|
||||||
// Jeśli sukces, zapisujemy token JWT [cite: 78]
|
|
||||||
if (response.data.status === "ok") {
|
|
||||||
localStorage.setItem('token', response.data.token);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response.data;
|
|
||||||
};
|
|
||||||
@@ -1,20 +1,35 @@
|
|||||||
// src/context/AuthProvider.tsx
|
import { useState, useCallback, type ReactNode } from 'react';
|
||||||
import { useState, useCallback, type ReactNode } from 'react';
|
import { AuthContext } from './AuthContext';
|
||||||
import { AuthContext } from './AuthContext'; // Importujemy stałą z pliku obok
|
|
||||||
import { sha512 } from '../utils/crypto';
|
import { sha512 } from '../utils/crypto';
|
||||||
import type { AuthResponse } from '../types/auth';
|
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 }) {
|
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||||
const [token, setToken] = useState<string | null>(sessionStorage.getItem('ktty_token'));
|
const [token, setToken] = useState<string | null>(sessionStorage.getItem('ktty_token'));
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
const authRequest = useCallback(async (endpoint: 'signUp' | 'signIn', name: string, pass: string) => {
|
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);
|
setLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
try {
|
try {
|
||||||
const hashedPassword = await sha512(pass);
|
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',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'accept': 'application/json',
|
'accept': 'application/json',
|
||||||
@@ -23,16 +38,26 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
|||||||
body: JSON.stringify({ name, password: hashedPassword, ttl: 86400 }),
|
body: JSON.stringify({ name, password: hashedPassword, ttl: 86400 }),
|
||||||
});
|
});
|
||||||
|
|
||||||
const data: AuthResponse = await response.json();
|
const contentType = response.headers.get("content-type");
|
||||||
if (!response.ok) throw new Error(data.error || data.message || `Error ${response.status}`);
|
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);
|
sessionStorage.setItem('ktty_token', data.token);
|
||||||
setToken(data.token); // To aktualizuje stan w całej aplikacji natychmiast!
|
setToken(data.token);
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
setError(err instanceof Error ? err.message : 'Unknown error');
|
const message = err instanceof Error ? err.message : 'Unknown error';
|
||||||
|
setError(message);
|
||||||
return null;
|
return null;
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|||||||
@@ -1,50 +1,29 @@
|
|||||||
import { defineConfig, loadEnv } from 'vite'
|
import { defineConfig, loadEnv } from 'vite'
|
||||||
import react from '@vitejs/plugin-react'
|
import react from '@vitejs/plugin-react'
|
||||||
import tailwindcss from '@tailwindcss/vite'
|
import tailwindcss from '@tailwindcss/vite'
|
||||||
import path from 'path' // Importuj moduł path
|
import path from 'path'
|
||||||
|
|
||||||
export default defineConfig(({ mode }) => {
|
export default defineConfig(({ mode }) => {
|
||||||
// Ustawiamy ścieżkę do folderu, w którym faktycznie znajduje się plik .env
|
// Ścieżka do folderu nadrzędnego
|
||||||
// path.resolve(__dirname, '..') oznacza: "wyjdź jeden poziom wyżej względem tego pliku"
|
|
||||||
const envDirectory = path.resolve(__dirname, '..');
|
const envDirectory = path.resolve(__dirname, '..');
|
||||||
|
|
||||||
// Ładujemy env z określonej lokalizacji
|
// Wczytujemy zmienne do użytku
|
||||||
const env = loadEnv(mode, envDirectory, '');
|
const env = loadEnv(mode, envDirectory, '');
|
||||||
|
|
||||||
const apiTarget = env.VITE_API_TARGET;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
||||||
|
envDir: envDirectory,
|
||||||
|
|
||||||
plugins: [
|
plugins: [
|
||||||
react(),
|
react(),
|
||||||
tailwindcss(),
|
tailwindcss(),
|
||||||
],
|
],
|
||||||
server: {
|
server: {
|
||||||
port: 6568,
|
port: 6568,
|
||||||
proxy: {
|
|
||||||
'/api': {
|
|
||||||
target: apiTarget,
|
|
||||||
changeOrigin: true,
|
|
||||||
secure: false,
|
|
||||||
headers: {
|
|
||||||
'Origin': apiTarget,
|
|
||||||
'Referer': `${apiTarget}/`
|
|
||||||
},
|
|
||||||
configure: (proxy) => {
|
|
||||||
proxy.on('error', (err) => {
|
|
||||||
console.log('[Proxy Error]:', err.message);
|
|
||||||
});
|
|
||||||
proxy.on('proxyReq', (_, req) => {
|
|
||||||
console.log(`[Proxy] Wysyłam do: ${apiTarget}${req.url}`);
|
|
||||||
});
|
|
||||||
proxy.on('proxyRes', (proxyRes, req) => {
|
|
||||||
console.log(`[Proxy] Odpowiedź: ${proxyRes.statusCode} ${req.url}`);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
preview: {
|
preview: {
|
||||||
port: 6568,
|
port: 6568,
|
||||||
|
allowedHosts: [env.VITE_ALLOWED_HOST],
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user