mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-05-14 18:25:54 +00:00
28 lines
865 B
JavaScript
28 lines
865 B
JavaScript
window.fetchWithCsrf = async function(url, options = {}) {
|
|
function getCsrfToken() {
|
|
const cookieValue = document.cookie
|
|
.split('; ')
|
|
.find(row => row.startsWith('XSRF-TOKEN='))
|
|
?.split('=')[1];
|
|
|
|
if (cookieValue) {
|
|
return cookieValue;
|
|
}
|
|
const csrfElement = document.querySelector('input[name="_csrf"]');
|
|
return csrfElement ? csrfElement.value : null;
|
|
}
|
|
|
|
// Create a new options object to avoid modifying the passed object
|
|
const fetchOptions = { ...options };
|
|
|
|
// Ensure headers object exists
|
|
fetchOptions.headers = { ...options.headers };
|
|
|
|
// Add CSRF token if available
|
|
const csrfToken = getCsrfToken();
|
|
if (csrfToken) {
|
|
fetchOptions.headers['X-XSRF-TOKEN'] = csrfToken;
|
|
}
|
|
|
|
return fetch(url, fetchOptions);
|
|
} |