auth.js 1.1 KB

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