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

@ -53,15 +53,15 @@ 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) {

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 },
{
withCredentials: true,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ login: username, password }), }
}); );
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,19 +2,17 @@ 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) {
throw new Error('Not authenticated');
} }
);
return await response.json(); return data;
} catch (err) { } catch (err) {
console.error('Auth check failed:', err); console.error('Auth check failed:', err);
return { isAuthenticated: false }; return { isAuthenticated: false };