mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-18 05:25:04 +00:00

# Description Please provide a summary of the changes, including relevant motivation and context. Closes #(issue_number) ## Checklist - [x] I have read the [Contribution Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md) - [x] I have performed a self-review of my own code - [ ] I have attached images of the change if it is UI based - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] If my code has heavily changed functionality I have updated relevant docs on [Stirling-PDFs doc repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/) - [x] My changes generate no new warnings - [ ] I have read the section [Add New Translation Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags) (for new translation tags only)
29 lines
850 B
JavaScript
29 lines
850 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);
|
|
}
|