25 lines
786 B
JavaScript
25 lines
786 B
JavaScript
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 };
|
||
}
|
||
}; |