import axios from "axios";
import axiosJWT from "../config/axios";

export const login = async (username, password) => {
	try {
		const data = {
			username,
			password,
		};

		const response = await axios.post("http://localhost:5000/login", data, {
			headers: {
				"Content-Type": "application/json",
			},
			withCredentials: true,
		});

		return response.data;
	} catch (error) {
		if (error.response) return error.response.data;
	}
};

export const refreshToken = async () => {
	try {
		const response = await axios.get("http://localhost:5000/token", { withCredentials: true });
		return response.data;
	} catch (error) {
		if (error.response) return error.response.data;
	}
};

export const getUser = async () => {
	try {
		const response = await axiosJWT.get("/user");
		return response.data;
	} catch (error) {
		if (error.response) return error.response.data;
	}
};

export const logout = async () => {
	try {
		const response = await axios.delete("http://localhost:5000/logout", {
			withCredentials: true,
		});
		return response.data;
	} catch (error) {
		if (error.response) return error.response.data;
	}
};
