import axios from "axios"

export const checkAuth = async () => {
  try {
    const { data } = await axios.get(
      `${import.meta.env.VITE_BACK_URL}/api/auth/check`,
      {
        withCredentials: true,
        headers: {
          'Authorization': `Bearer ${localStorage.getItem('access_token') || ''}`,
        },
      }
    );

    console.log('Auth check response:', data);

    if (!data.user) {
      return { isAuthenticated: false };
    }

    return {
      isAuthenticated: data.isAuthenticated,
      user: {
        id: data.user.id,
        login: data.user.login,
        role: data.user.role
      }
    };
  } catch (err) {
    console.error('Auth check failed:', err);
    return { isAuthenticated: false };
  }
};