From 78daea30ac4d39a7fae7b2310dfb8dc2cdc4e3fc Mon Sep 17 00:00:00 2001 From: Marcin-Ramotowski Date: Sun, 13 Apr 2025 13:01:07 +0200 Subject: [PATCH] Refactored code for api methods --- frontend/src/api/api.ts | 32 ++------------------------------ frontend/src/api/auth.ts | 34 ++++++++++++++++++---------------- frontend/src/api/tasks.ts | 11 ++++------- frontend/src/pages/Login.tsx | 2 +- 4 files changed, 25 insertions(+), 54 deletions(-) diff --git a/frontend/src/api/api.ts b/frontend/src/api/api.ts index 19fcc24..18cc4a4 100644 --- a/frontend/src/api/api.ts +++ b/frontend/src/api/api.ts @@ -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; diff --git a/frontend/src/api/auth.ts b/frontend/src/api/auth.ts index 33eedbf..a9466ad 100644 --- a/frontend/src/api/auth.ts +++ b/frontend/src/api/auth.ts @@ -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; -}; \ No newline at end of file +// Logout +export const logout = async () => { + await api.get("/logout"); // API removes JWT + Cookies.remove("access_token_cookie"); + Cookies.remove("user_id"); +}; diff --git a/frontend/src/api/tasks.ts b/frontend/src/api/tasks.ts index 6948f9e..c1d1611 100644 --- a/frontend/src/api/tasks.ts +++ b/frontend/src/api/tasks.ts @@ -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}`) }; diff --git a/frontend/src/pages/Login.tsx b/frontend/src/pages/Login.tsx index 53b6307..570a8b3 100644 --- a/frontend/src/pages/Login.tsx +++ b/frontend/src/pages/Login.tsx @@ -1,6 +1,6 @@ import { useState } from "react"; import { useNavigate } from "react-router-dom"; -import { login } from "../api/api"; +import { login } from "../api/auth"; const Login = () => { const [username, setUsername] = useState("");