Started implementation of basic frontend for ToDoList App
This commit is contained in:
44
frontend/src/api/api.ts
Normal file
44
frontend/src/api/api.ts
Normal file
@ -0,0 +1,44 @@
|
||||
import axios from "axios";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
const API_URL = "http://localhost:5000";
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_URL,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
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");
|
||||
};
|
22
frontend/src/api/auth.ts
Normal file
22
frontend/src/api/auth.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import axios from "axios";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
const API_URL = "http://localhost:5000";
|
||||
|
||||
export const logout = async () => {
|
||||
try {
|
||||
await axios.get(`${API_URL}/logout`, { withCredentials: true });
|
||||
} catch (error) {
|
||||
console.error("Error during logout:", error);
|
||||
}
|
||||
|
||||
// Remove JWT token from cookies
|
||||
document.cookie = "access_token_cookie=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
||||
localStorage.removeItem("user_id");
|
||||
};
|
||||
|
||||
export const getCsrfToken = () => {
|
||||
const value = Cookies.get("csrf_access_token");
|
||||
const header = {"X-CSRF-TOKEN": value}
|
||||
return header;
|
||||
};
|
26
frontend/src/api/tasks.ts
Normal file
26
frontend/src/api/tasks.ts
Normal file
@ -0,0 +1,26 @@
|
||||
import axios from "axios";
|
||||
import { getCsrfToken } from "./auth";
|
||||
|
||||
const API_URL = "http://localhost:5000";
|
||||
|
||||
// Get user tasks
|
||||
export const getUserTasks = async (userId: number) => {
|
||||
const response = await axios.get(`${API_URL}/tasks/user/${userId}`, {withCredentials: true, headers: getCsrfToken()});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// Create new task
|
||||
export const createTask = async (taskData: {
|
||||
title: string;
|
||||
description: string;
|
||||
due_date: string;
|
||||
done: boolean;
|
||||
}) => {
|
||||
const response = await axios.post(`${API_URL}/tasks`, taskData, {withCredentials: true, headers: getCsrfToken()});
|
||||
return response.data;
|
||||
};
|
||||
|
||||
// Delete task
|
||||
export const deleteTask = async (taskId: number) => {
|
||||
await axios.delete(`${API_URL}/tasks/${taskId}`, {withCredentials: true, headers: getCsrfToken()});
|
||||
};
|
Reference in New Issue
Block a user