05/24 Fix bugs CSRF error
This commit is contained in:
+5
-3
@@ -198,10 +198,12 @@ function getCsrfToken() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ── Auto-inject CSRF token into every static POST form ─────── */
|
/* ── Auto-inject CSRF token into every static POST form ─────── */
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
/* Called immediately — app.js is at the end of <body> so all */
|
||||||
|
/* forms are already in the DOM when this executes. */
|
||||||
|
(function injectCsrf() {
|
||||||
const token = getCsrfToken();
|
const token = getCsrfToken();
|
||||||
if (!token) return;
|
if (!token) return;
|
||||||
document.querySelectorAll('form').forEach(form => {
|
document.querySelectorAll('form').forEach(function(form) {
|
||||||
if ((form.getAttribute('method') || '').toLowerCase() !== 'post') return;
|
if ((form.getAttribute('method') || '').toLowerCase() !== 'post') return;
|
||||||
if (form.querySelector('input[name="csrf_token"]')) return;
|
if (form.querySelector('input[name="csrf_token"]')) return;
|
||||||
const input = document.createElement('input');
|
const input = document.createElement('input');
|
||||||
@@ -210,7 +212,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
input.value = token;
|
input.value = token;
|
||||||
form.appendChild(input);
|
form.appendChild(input);
|
||||||
});
|
});
|
||||||
});
|
}());
|
||||||
|
|
||||||
/* ── Generic fetch-based form submit (JSON response) ───────── */
|
/* ── Generic fetch-based form submit (JSON response) ───────── */
|
||||||
async function submitJson(url, data, method = 'POST') {
|
async function submitJson(url, data, method = 'POST') {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<form method="post" action="{{ url_for('auth.login') }}">
|
<form method="post" action="{{ url_for('auth.login') }}">
|
||||||
|
<input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="username">Username</label>
|
<label for="username">Username</label>
|
||||||
<input type="text" id="username" name="username"
|
<input type="text" id="username" name="username"
|
||||||
|
|||||||
Reference in New Issue
Block a user