Added environment variables
test-org/trust-module-frontend/pipeline/pr-main This commit looks good Details
test-org/trust-module-frontend/pipeline/pr-rc This commit looks good Details

pull/40/head
DmitriyA 2025-05-28 01:47:18 -04:00
parent 4088dacba4
commit 8223cc4a27
3 changed files with 39 additions and 41 deletions

View File

@ -52,22 +52,22 @@ function App() {
}; };
const handleLogout = async () => { const handleLogout = async () => {
try { try {
await fetch('http://192.168.2.39:3000/api/auth/logout', { await axios.post(`${import.meta.env.VITE_BACK_URL}/api/auth/logout`, null, {
method: 'POST', withCredentials: true, // чтобы отправлялись куки
credentials: 'include' });
});
localStorage.removeItem('access_token'); localStorage.removeItem('access_token');
setAuthState({ setAuthState({
isAuthenticated: false, isAuthenticated: false,
isLoading: false, isLoading: false,
user: null user: null,
}); });
setShowLoginModal(true); setShowLoginModal(true);
} catch (error) { } catch (error) {
console.error('Logout failed:', error); console.error('Logout failed:', error);
} }
}; };
// Полноэкранный лоадер во время проверки авторизации // Полноэкранный лоадер во время проверки авторизации
if (authState.isLoading) { if (authState.isLoading) {

View File

@ -17,16 +17,16 @@ const LoginModal = ({ onLogin, onClose }) => {
e.preventDefault(); e.preventDefault();
try { try {
const response = await fetch('http://192.168.2.39:3000/api/auth/login', { const { data } = await axios.post(
method: 'POST', `${import.meta.env.VITE_BACK_URL}/api/auth/login`,
credentials: 'include', { login: username, password },
headers: { {
'Content-Type': 'application/json', withCredentials: true,
}, headers: {
body: JSON.stringify({ login: username, password }), 'Content-Type': 'application/json',
}); },
}
const data = await response.json(); );
if (data.success) { if (data.success) {
localStorage.setItem('access_token', data.access_token); localStorage.setItem('access_token', data.access_token);
@ -37,7 +37,7 @@ const LoginModal = ({ onLogin, onClose }) => {
} }
} catch (err) { } catch (err) {
console.error('Ошибка при отправке запроса:', err); console.error('Ошибка при отправке запроса:', err);
setError("Ошибка при подключении к серверу"); setError(err.response?.data?.message || "Ошибка при подключении к серверу");
} }
}; };

View File

@ -2,21 +2,19 @@ import axios from 'axios';
export const checkAuth = async () => { export const checkAuth = async () => {
try { try {
const response = await fetch('http://192.168.2.39:3000/api/auth/check', { const { data } = await axios.get(
method: 'GET', `${import.meta.env.VITE_BACK_URL}/api/auth/check`,
credentials: 'include', // Важно для отправки cookies {
withCredentials: true,
headers: { headers: {
'Authorization': `Bearer ${localStorage.getItem('access_token') || ''}`, 'Authorization': `Bearer ${localStorage.getItem('access_token') || ''}`,
}, },
}); }
);
if (!response.ok) { return data;
throw new Error('Not authenticated'); } catch (err) {
} console.error('Auth check failed:', err);
return { isAuthenticated: false };
return await response.json();
} catch (err) {
console.error('Auth check failed:', err);
return { isAuthenticated: false };
} }
}; };