Added environment variables
parent
4088dacba4
commit
8223cc4a27
10
src/App.jsx
10
src/App.jsx
|
|
@ -53,21 +53,21 @@ function App() {
|
|||
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
await fetch('http://192.168.2.39:3000/api/auth/logout', {
|
||||
method: 'POST',
|
||||
credentials: 'include'
|
||||
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
|
||||
user: null,
|
||||
});
|
||||
setShowLoginModal(true);
|
||||
} catch (error) {
|
||||
console.error('Logout failed:', error);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
// Полноэкранный лоадер во время проверки авторизации
|
||||
if (authState.isLoading) {
|
||||
|
|
|
|||
|
|
@ -17,16 +17,16 @@ const LoginModal = ({ onLogin, onClose }) => {
|
|||
e.preventDefault();
|
||||
|
||||
try {
|
||||
const response = await fetch('http://192.168.2.39:3000/api/auth/login', {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
const { data } = await axios.post(
|
||||
`${import.meta.env.VITE_BACK_URL}/api/auth/login`,
|
||||
{ login: username, password },
|
||||
{
|
||||
withCredentials: true,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ login: username, password }),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
}
|
||||
);
|
||||
|
||||
if (data.success) {
|
||||
localStorage.setItem('access_token', data.access_token);
|
||||
|
|
@ -37,7 +37,7 @@ const LoginModal = ({ onLogin, onClose }) => {
|
|||
}
|
||||
} catch (err) {
|
||||
console.error('Ошибка при отправке запроса:', err);
|
||||
setError("Ошибка при подключении к серверу");
|
||||
setError(err.response?.data?.message || "Ошибка при подключении к серверу");
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -2,20 +2,18 @@ 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') || ''}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Not authenticated');
|
||||
}
|
||||
);
|
||||
|
||||
return await response.json();
|
||||
} catch (err) {
|
||||
return data;
|
||||
} catch (err) {
|
||||
console.error('Auth check failed:', err);
|
||||
return { isAuthenticated: false };
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue