From 8223cc4a27ae72dfde91e26f797fb70491c36cf0 Mon Sep 17 00:00:00 2001 From: DmitriyA Date: Wed, 28 May 2025 01:47:18 -0400 Subject: [PATCH] Added environment variables --- src/App.jsx | 32 ++++++++++++++++---------------- src/Components/UI/LoginModal.jsx | 24 ++++++++++++------------ src/Components/UI/auth.jsx | 24 +++++++++++------------- 3 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index abbe919..798e186 100755 --- a/src/App.jsx +++ b/src/App.jsx @@ -52,22 +52,22 @@ function App() { }; const handleLogout = async () => { - try { - await fetch('http://192.168.2.39:3000/api/auth/logout', { - method: 'POST', - credentials: 'include' - }); - localStorage.removeItem('access_token'); - setAuthState({ - isAuthenticated: false, - isLoading: false, - user: null - }); - setShowLoginModal(true); - } catch (error) { - console.error('Logout failed:', error); - } - }; + try { + await axios.post(`${import.meta.env.VITE_BACK_URL}/api/auth/logout`, null, { + withCredentials: true, // чтобы отправлялись куки + }); + + localStorage.removeItem('access_token'); + setAuthState({ + isAuthenticated: false, + isLoading: false, + user: null, + }); + setShowLoginModal(true); + } catch (error) { + console.error('Logout failed:', error); + } +}; // Полноэкранный лоадер во время проверки авторизации if (authState.isLoading) { diff --git a/src/Components/UI/LoginModal.jsx b/src/Components/UI/LoginModal.jsx index 22559d6..f76b7b3 100755 --- a/src/Components/UI/LoginModal.jsx +++ b/src/Components/UI/LoginModal.jsx @@ -17,27 +17,27 @@ const LoginModal = ({ onLogin, onClose }) => { e.preventDefault(); try { - const response = await fetch('http://192.168.2.39:3000/api/auth/login', { - method: 'POST', - credentials: 'include', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ login: username, password }), - }); - - const data = await response.json(); + const { data } = await axios.post( + `${import.meta.env.VITE_BACK_URL}/api/auth/login`, + { login: username, password }, + { + withCredentials: true, + headers: { + 'Content-Type': 'application/json', + }, + } + ); if (data.success) { localStorage.setItem('access_token', data.access_token); - onLogin(data.user); + onLogin(data.user); onClose(); } else { setError(data.message || "Неверный логин или пароль"); } } catch (err) { console.error('Ошибка при отправке запроса:', err); - setError("Ошибка при подключении к серверу"); + setError(err.response?.data?.message || "Ошибка при подключении к серверу"); } }; diff --git a/src/Components/UI/auth.jsx b/src/Components/UI/auth.jsx index 6d3699c..c0beca8 100644 --- a/src/Components/UI/auth.jsx +++ b/src/Components/UI/auth.jsx @@ -2,21 +2,19 @@ import axios from 'axios'; export const checkAuth = async () => { try { - const response = await fetch('http://192.168.2.39:3000/api/auth/check', { - method: 'GET', - credentials: 'include', // Важно для отправки cookies + const { data } = await axios.get( + `${import.meta.env.VITE_BACK_URL}/api/auth/check`, + { + withCredentials: true, headers: { - 'Authorization': `Bearer ${localStorage.getItem('access_token') || ''}`, + 'Authorization': `Bearer ${localStorage.getItem('access_token') || ''}`, }, - }); + } + ); - if (!response.ok) { - throw new Error('Not authenticated'); - } - - return await response.json(); -} catch (err) { - console.error('Auth check failed:', err); - return { isAuthenticated: false }; + return data; + } catch (err) { + console.error('Auth check failed:', err); + return { isAuthenticated: false }; } }; \ No newline at end of file