Refactored code for api methods
This commit is contained in:
parent
1ed7f308a3
commit
78daea30ac
@ -8,37 +8,9 @@ const api = axios.create({
|
|||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
Accept: "application/json",
|
Accept: "application/json",
|
||||||
|
"X-CSRF-TOKEN": Cookies.get("csrf_access_token")
|
||||||
},
|
},
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// User login
|
export default api;
|
||||||
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");
|
|
||||||
};
|
|
||||||
|
@ -1,22 +1,24 @@
|
|||||||
import axios from "axios";
|
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
|
import api from "./api";
|
||||||
|
|
||||||
const API_URL = "http://localhost:5000";
|
// User login
|
||||||
|
export const login = async (username: string, password: string) => {
|
||||||
export const logout = async () => {
|
|
||||||
try {
|
try {
|
||||||
await axios.get(`${API_URL}/logout`, { withCredentials: true });
|
const response = await api.post("/login", { username, password });
|
||||||
} catch (error) {
|
|
||||||
console.error("Error during logout:", error);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove JWT token from cookies
|
const userId = response.data.user_id;
|
||||||
document.cookie = "access_token_cookie=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
|
|
||||||
localStorage.removeItem("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 = () => {
|
// Logout
|
||||||
const value = Cookies.get("csrf_access_token");
|
export const logout = async () => {
|
||||||
const header = {"X-CSRF-TOKEN": value}
|
await api.get("/logout"); // API removes JWT
|
||||||
return header;
|
Cookies.remove("access_token_cookie");
|
||||||
};
|
Cookies.remove("user_id");
|
||||||
|
};
|
||||||
|
@ -1,11 +1,8 @@
|
|||||||
import axios from "axios";
|
import api from "./api"
|
||||||
import { getCsrfToken } from "./auth";
|
|
||||||
|
|
||||||
const API_URL = "http://localhost:5000";
|
|
||||||
|
|
||||||
// Get user tasks
|
// Get user tasks
|
||||||
export const getUserTasks = async (userId: number) => {
|
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;
|
return response.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -16,11 +13,11 @@ export const createTask = async (taskData: {
|
|||||||
due_date: string;
|
due_date: string;
|
||||||
done: boolean;
|
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;
|
return response.data;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Delete task
|
// Delete task
|
||||||
export const deleteTask = async (taskId: number) => {
|
export const deleteTask = async (taskId: number) => {
|
||||||
await axios.delete(`${API_URL}/tasks/${taskId}`, {withCredentials: true, headers: getCsrfToken()});
|
await api.delete(`/tasks/${taskId}`)
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { login } from "../api/api";
|
import { login } from "../api/auth";
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
const [username, setUsername] = useState("");
|
const [username, setUsername] = useState("");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user