Refactored code for api methods
This commit is contained in:
		@@ -8,37 +8,9 @@ const api = axios.create({
 | 
			
		||||
  headers: {
 | 
			
		||||
    "Content-Type": "application/json",
 | 
			
		||||
    Accept: "application/json",
 | 
			
		||||
    "X-CSRF-TOKEN": Cookies.get("csrf_access_token")
 | 
			
		||||
  },
 | 
			
		||||
  withCredentials: true,
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// User login
 | 
			
		||||
export const login = async (username: string, password: string) => {
 | 
			
		||||
  try {
 | 
			
		||||
    const response = await api.post("/login", { username, password });
 | 
			
		||||
 | 
			
		||||
    const userId = response.data.user_id;
 | 
			
		||||
 | 
			
		||||
    Cookies.set("user_id", String(userId), { secure: true, sameSite: "Strict" });
 | 
			
		||||
 | 
			
		||||
    return { userId };
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    throw new Error("Incorrect username or password.");
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Get user tasks
 | 
			
		||||
export const getTasks = async () => {
 | 
			
		||||
  const userId = Cookies.get("user_id");
 | 
			
		||||
  if (!userId) throw new Error("No user_id in cookies.");
 | 
			
		||||
 | 
			
		||||
  const response = await api.get(`/tasks/user/${userId}`);
 | 
			
		||||
  return response.data;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Logout
 | 
			
		||||
export const logout = async () => {
 | 
			
		||||
  await api.get("/logout"); // API usunie JWT
 | 
			
		||||
  Cookies.remove("access_token_cookie");
 | 
			
		||||
  Cookies.remove("user_id");
 | 
			
		||||
};
 | 
			
		||||
export default api;
 | 
			
		||||
 
 | 
			
		||||
@@ -1,22 +1,24 @@
 | 
			
		||||
import axios from "axios";
 | 
			
		||||
import Cookies from "js-cookie";
 | 
			
		||||
import api from "./api";
 | 
			
		||||
 | 
			
		||||
const API_URL = "http://localhost:5000";
 | 
			
		||||
 | 
			
		||||
export const logout = async () => {
 | 
			
		||||
// User login
 | 
			
		||||
export const login = async (username: string, password: string) => {
 | 
			
		||||
  try {
 | 
			
		||||
    await axios.get(`${API_URL}/logout`, { withCredentials: true });
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    console.error("Error during logout:", error);
 | 
			
		||||
  }
 | 
			
		||||
    const response = await api.post("/login", { username, password });
 | 
			
		||||
 | 
			
		||||
  // Remove JWT token from cookies
 | 
			
		||||
  document.cookie = "access_token_cookie=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
 | 
			
		||||
  localStorage.removeItem("user_id");
 | 
			
		||||
    const userId = response.data.user_id;
 | 
			
		||||
 | 
			
		||||
    Cookies.set("user_id", String(userId), { secure: true, sameSite: "Strict" });
 | 
			
		||||
 | 
			
		||||
    return { userId };
 | 
			
		||||
  } catch (error) {
 | 
			
		||||
    throw new Error("Incorrect username or password.");
 | 
			
		||||
  }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const getCsrfToken = () => {
 | 
			
		||||
  const value = Cookies.get("csrf_access_token");
 | 
			
		||||
  const header = {"X-CSRF-TOKEN": value}
 | 
			
		||||
  return header;
 | 
			
		||||
};
 | 
			
		||||
// Logout
 | 
			
		||||
export const logout = async () => {
 | 
			
		||||
  await api.get("/logout"); // API removes JWT
 | 
			
		||||
  Cookies.remove("access_token_cookie");
 | 
			
		||||
  Cookies.remove("user_id");
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,8 @@
 | 
			
		||||
import axios from "axios";
 | 
			
		||||
import { getCsrfToken } from "./auth";
 | 
			
		||||
 | 
			
		||||
const API_URL = "http://localhost:5000";
 | 
			
		||||
import api from "./api"
 | 
			
		||||
 | 
			
		||||
// Get user tasks
 | 
			
		||||
export const getUserTasks = async (userId: number) => {
 | 
			
		||||
  const response = await axios.get(`${API_URL}/tasks/user/${userId}`, {withCredentials: true, headers: getCsrfToken()});
 | 
			
		||||
  const response = await api.get(`/tasks/user/${userId}`);
 | 
			
		||||
  return response.data;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -16,11 +13,11 @@ export const createTask = async (taskData: {
 | 
			
		||||
  due_date: string;
 | 
			
		||||
  done: boolean;
 | 
			
		||||
}) => {
 | 
			
		||||
  const response = await axios.post(`${API_URL}/tasks`, taskData, {withCredentials: true, headers: getCsrfToken()});
 | 
			
		||||
  const response = await api.post("/tasks", taskData);
 | 
			
		||||
  return response.data;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Delete task
 | 
			
		||||
export const deleteTask = async (taskId: number) => {
 | 
			
		||||
  await axios.delete(`${API_URL}/tasks/${taskId}`, {withCredentials: true, headers: getCsrfToken()});
 | 
			
		||||
  await api.delete(`/tasks/${taskId}`)
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user