From b4d653f3a6abfcdc5735e03ac9609e14bd502fe4 Mon Sep 17 00:00:00 2001 From: DmitriyA Date: Mon, 21 Apr 2025 09:01:36 -0400 Subject: [PATCH] corrections after the code review --- src/App.jsx | 3 ++- src/Components/UI/LoginModal.jsx | 4 +++- src/Components/UI/auth.jsx | 37 ++++++++++++++++++-------------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 73fbf52..c2372e5 100755 --- a/src/App.jsx +++ b/src/App.jsx @@ -5,6 +5,7 @@ import LoginModal from "./Components/UI/LoginModal"; import { lightTheme, darkTheme } from "./Style/theme"; import Logo from './assets/images/logo.svg?react'; import { checkAuth } from "./Components/UI/auth"; +import axios from 'axios'; function App() { const [authState, setAuthState] = useState({ @@ -56,7 +57,7 @@ function App() { const handleLogout = async () => { try { - await fetch('http://192.168.2.39:3000/api/auth/logout', { + await axios.get(`${import.meta.env.VITE_BACK_URL}/api/metrics`, { method: 'POST', credentials: 'include' }); diff --git a/src/Components/UI/LoginModal.jsx b/src/Components/UI/LoginModal.jsx index 5a3bfc8..d38d7b7 100755 --- a/src/Components/UI/LoginModal.jsx +++ b/src/Components/UI/LoginModal.jsx @@ -3,6 +3,7 @@ import Modal from "./Modal"; import "../../Style/LoginModal.css"; import Logo from '../../assets/images/logo.svg?react'; import TextField from '@mui/material/TextField'; +import axios from 'axios'; const LoginModal = ({ onLogin, onClose }) => { const [username, setUsername] = useState(""); @@ -16,7 +17,8 @@ const LoginModal = ({ onLogin, onClose }) => { e.preventDefault(); try { - const response = await fetch('http://192.168.2.39:3000/api/auth/login', { + const response = await axios.post( + `${import.meta.env.VITE_BACK_URL}/api/auth/login`, { method: 'POST', credentials: 'include', headers: { diff --git a/src/Components/UI/auth.jsx b/src/Components/UI/auth.jsx index 9c8e0a9..7532538 100644 --- a/src/Components/UI/auth.jsx +++ b/src/Components/UI/auth.jsx @@ -1,20 +1,25 @@ +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 - headers: { - 'Authorization': `Bearer ${localStorage.getItem('access_token') || ''}`, - }, - }); + try { + const response = await axios.get( + `${import.meta.env.VITE_BACK_URL}/api/auth/check`, + { + withCredentials: true, // аналог `credentials: 'include'` в fetch + headers: { + 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 }; + // У axios нет свойства .ok, проверяем статус 200-299 + if (response.status >= 200 && response.status < 300) { + return response.data; // Данные уже в JSON, не нужно .json() + } else { + throw new Error('Not authenticated'); } + } catch (err) { + console.error('Auth check failed:', err); + return { isAuthenticated: false }; + } }; \ No newline at end of file