From 17ffb4e6b1e92c432996644287497f3231644ffe Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Tue, 3 Jun 2025 13:59:02 +0545 Subject: [PATCH] refactor(admin_dashboard.js): improve getCSRFToken function with additional token sources --- static/js/admin_dashboard.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/static/js/admin_dashboard.js b/static/js/admin_dashboard.js index 2a2aeb2..5fa33df 100644 --- a/static/js/admin_dashboard.js +++ b/static/js/admin_dashboard.js @@ -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/', {