Fix CSRF token retrieval for AJAX requests
- Use getCsrfToken() function that follows same logic as base.js - Properly retrieve CSRF token from meta tag, body data, input or cookie
This commit is contained in:
parent
a9b08bdc6e
commit
735ebd8bd7
|
|
@ -227,6 +227,37 @@
|
|||
{% block scripts %}
|
||||
{{ parent() }}
|
||||
<script>
|
||||
function getCsrfToken() {
|
||||
// 1. Пробуем из мета-тега
|
||||
const meta = document.querySelector('meta[name="csrf-token"]');
|
||||
if (meta && meta.getAttribute('content')) {
|
||||
return meta.getAttribute('content');
|
||||
}
|
||||
|
||||
// 2. Пробуем из data-атрибута body
|
||||
if (document.body && document.body.dataset.csrfToken) {
|
||||
return document.body.dataset.csrfToken;
|
||||
}
|
||||
|
||||
// 3. Пробуем из скрытого input на странице
|
||||
const csrfInput = document.querySelector('input[name*="csrf"]');
|
||||
if (csrfInput && csrfInput.value) {
|
||||
return csrfInput.value;
|
||||
}
|
||||
|
||||
// 4. Пробуем из cookie
|
||||
const cookies = document.cookie.split(';');
|
||||
for (let cookie of cookies) {
|
||||
const [name, value] = cookie.trim().split('=');
|
||||
if (name === 'csrf_cookie_name' && value) {
|
||||
return decodeURIComponent(value);
|
||||
}
|
||||
}
|
||||
|
||||
console.warn('CSRF token not found anywhere');
|
||||
return '';
|
||||
}
|
||||
|
||||
function addSubtask(event, taskId) {
|
||||
event.preventDefault();
|
||||
const form = event.target;
|
||||
|
|
@ -235,8 +266,7 @@ function addSubtask(event, taskId) {
|
|||
|
||||
if (!title) return;
|
||||
|
||||
// Получаем CSRF токен из формы
|
||||
const csrfToken = form.querySelector('input[name="csrf_token"]').value;
|
||||
const csrfToken = getCsrfToken();
|
||||
|
||||
fetch(`/tasks/${taskId}/subtasks`, {
|
||||
method: 'POST',
|
||||
|
|
@ -292,9 +322,7 @@ function updateSubtasksCount() {
|
|||
}
|
||||
|
||||
function toggleSubtask(taskId, subtaskId) {
|
||||
// Получаем CSRF токен из формы на странице
|
||||
const csrfForm = document.querySelector('form[action*="/tasks/' + taskId + '"]');
|
||||
const csrfToken = csrfForm ? csrfForm.querySelector('input[name="csrf_token"]').value : '';
|
||||
const csrfToken = getCsrfToken();
|
||||
|
||||
fetch(`/tasks/${taskId}/subtasks/${subtaskId}/toggle`, {
|
||||
method: 'POST',
|
||||
|
|
@ -321,9 +349,7 @@ function toggleSubtask(taskId, subtaskId) {
|
|||
function deleteSubtask(taskId, subtaskId) {
|
||||
if (!confirm('Удалить подзадачу?')) return;
|
||||
|
||||
// Получаем CSRF токен из формы на странице
|
||||
const csrfForm = document.querySelector('form[action*="/tasks/' + taskId + '"]');
|
||||
const csrfToken = csrfForm ? csrfForm.querySelector('input[name="csrf_token"]').value : '';
|
||||
const csrfToken = getCsrfToken();
|
||||
|
||||
fetch(`/tasks/${taskId}/subtasks/${subtaskId}/delete`, {
|
||||
method: 'POST',
|
||||
|
|
|
|||
Loading…
Reference in New Issue