Skip to content
Snippets Groups Projects
api.js 1.4 KiB
Newer Older
Marc Taylor's avatar
Marc Taylor committed
// 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;


export const login = async (username, password) => {
  try {
    const response = await axios.post('http://127.0.0.1:8000/themeApp/login/', {
      username: username,
      password: password
    }, {
      headers: {
        'Content-Type': 'application/json'
      },
      withCredentials: true  // Include cookies in the request
    });
    console.log('Login success:', response.data);
    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)
  }
}

Marc Taylor's avatar
Marc Taylor committed
// Other API calls can be added here