feat: update csrf on http base services

This commit is contained in:
hanif salafi 2025-01-10 14:15:30 +07:00
parent 9bce4dd24f
commit c8f95aa101
2 changed files with 30 additions and 11 deletions

View File

@ -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);
}

View File

@ -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;