trust-module-frontend/src/Components/UI/auth.jsx

25 lines
786 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import axios from 'axios';
export const checkAuth = async () => {
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') || ''}`,
},
}
);
// У 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 };
}
};