diff --git a/service/auth.ts b/service/auth.ts index 2e02133e..56a86015 100644 --- a/service/auth.ts +++ b/service/auth.ts @@ -8,18 +8,10 @@ import { } from "./http-config/http-interceptor-service"; export async function login(data: any) { - - const res = await getCsrfToken(); - const csrfToken = res?.data?.token; - - - console.log("Token CSRF : ", csrfToken); - const pathUrl = "signin"; const headers = { 'accept': 'application/json', 'content-type': 'application/json', - 'X-XSRF-TOKEN': csrfToken }; return httpPost(pathUrl, headers, data); } diff --git a/service/http-config/http-base-service.ts b/service/http-config/http-base-service.ts index 2fe098c2..2d4ed979 100644 --- a/service/http-config/http-base-service.ts +++ b/service/http-config/http-base-service.ts @@ -2,10 +2,24 @@ import axiosBaseInstance from "./axios-base-instance"; import axios from "axios"; import Cookies from "js-cookie"; import qs from "qs"; +import { getCsrfToken } from "../auth"; export async function httpPost(pathUrl: any, headers: any, data?: any) { - const response = await axiosBaseInstance - .post(pathUrl, data, { headers }) + const resCsrf = await getCsrfToken(); + const csrfToken = resCsrf?.data?.token; + + const defaultHeaders = { + "Content-Type": "application/json", + }; + + const mergedHeaders = { + ...defaultHeaders, + ...headers, + ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), + }; + + const response = await axiosBaseInstance + .post(pathUrl, data, { headers: mergedHeaders }) .catch(function (error: any) { console.log(error); return error.response; @@ -50,8 +64,21 @@ export async function httpGet(pathUrl: any, headers: any) { } export async function httpPut(pathUrl: any, headers: any, data?: any) { + const resCsrf = await getCsrfToken(); + const csrfToken = resCsrf?.data?.token; + + const defaultHeaders = { + "Content-Type": "application/json", + }; + + const mergedHeaders = { + ...defaultHeaders, + ...headers, + ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), + }; + const response = await axiosBaseInstance - .put(pathUrl, data, { headers }) + .put(pathUrl, data, { headers: mergedHeaders }) .catch(function (error: any) { console.log(error); return error.response;