// src/utils/api.js import axios from 'axios'; const API_BASE_URL = 'http://127.0.0.1:8000'; axios.defaults.xsrfCookieName = 'csrftoken'; axios.defaults.xsrfHeaderName = 'X-CSRFToken'; axios.defaults.withCredentials = true; const TOKEN = localStorage.getItem("access_token"); // Replace with your Bearer token export const login = async (username, password) => { try { const response = await axios.post('http://127.0.0.1:8000/token/', { username: username, password: password }, { headers: { 'Content-Type': 'application/json' }, withCredentials: true // Include cookies in the request }); localStorage.clear(); localStorage.setItem('access_token', response.data.access); localStorage.setItem('refresh_token', response.data.refresh); console.log('Login success:', response); return response.status } catch (error) { console.error('Login error:', error); } } export const addProduct = async (img, price, rentPrice, name, size, userID) => { const formData = new FormData(); formData.append("image", img) formData.append("price", price) formData.append("rent_price", rentPrice) formData.append("name", name) formData.append("size", size) formData.append("user_id", userID) try { const response = await axios.post('http://127.0.0.1:8000/themeApp/products/', formData, { headers: { 'Content-Type': 'multipart/form-data' }, }); console.log('Added product successfully:', response.data); return response.status } catch (error) { console.error('Error in adding product:', error) } } export const getLoggedInUser = async () => { try { const {data} = await axios.get('http://localhost:8000/themeApp/user/', { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${TOKEN}` } }); return data.user; } catch (e) { console.log('not auth', e) return false; } } export const getUserProducts = async () => { const user = await getLoggedInUser(); console.log(user.id, 'userID'); // const allProducts = await } // getUserProducts(); // Other API calls can be added here