refactor(admin_dashboard.js): improve getCSRFToken function with additional token sources

This commit is contained in:
sankalp committed 2025-06-03 13:59:02 +05:45
1 parent 6d5d58ef8d
commit 17ffb4e6b1
1 file changed
+27 -2
+27 -2
View File
@@ -1,12 +1,37 @@
// Function to get CSRF token
function getCSRFToken() {
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
return token;
// Try getting from meta tag
const metaTag = document.querySelector('meta[name="csrf-token"]');
if (metaTag) {
return metaTag.getAttribute('content');
}
// Try getting from form input
const inputField = document.querySelector('input[name="csrfmiddlewaretoken"]');
if (inputField) {
return inputField.value;
}
// Try getting from cookie
const cookies = document.cookie.split(';');
for (let cookie of cookies) {
cookie = cookie.trim();
if (cookie.startsWith('csrftoken=')) {
return decodeURIComponent(cookie.substring('csrftoken='.length));
}
}
console.error('CSRF token not found in meta tag, form input, or cookie');
return null;
}
// Function to update leave requests
function updateLeaveRequests() {
const csrfToken = getCSRFToken();
if (!csrfToken) {
console.error('No CSRF token available. Cannot make request.');
return;
}
// Fetch staff leave requests
fetch('/app/get-staff-leaves/', {