auth.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import axios from "axios";
  2. import axiosJWT from "../config/axios";
  3. import qs from "qs";
  4. export const login = async (username, password) => {
  5. try {
  6. const data = qs.stringify({
  7. username,
  8. password,
  9. });
  10. const response = await axios.post("http://localhost:5000/login", data, {
  11. headers: {
  12. "Content-Type": "application/x-www-form-urlencoded",
  13. },
  14. withCredentials: true,
  15. });
  16. return response.data;
  17. } catch (error) {
  18. if (error.response) return error.response.data;
  19. }
  20. };
  21. export const refreshToken = async () => {
  22. try {
  23. const response = await axios.get("http://localhost:5000/token", { withCredentials: true });
  24. return response.data;
  25. } catch (error) {
  26. if (error.response) return error.response.data;
  27. }
  28. };
  29. export const getUser = async () => {
  30. try {
  31. const response = await axiosJWT.get("http://localhost:5000/user");
  32. return response.data;
  33. } catch (error) {
  34. if (error.response) return error.response.data;
  35. }
  36. };
  37. export const logout = async () => {
  38. try {
  39. const response = await axios.delete("http://localhost:5000/logout", {
  40. withCredentials: true,
  41. });
  42. return response.data;
  43. } catch (error) {
  44. if (error.response) return error.response.data;
  45. }
  46. };