chat: брать CSRF из hx-headers — HttpOnly cookie ломала POST /chat/api

Иначе Django отдавал 403 на /users/ids, posts, channels/direct и т.п.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-09-22 13:00:31 +03:00
parent 4c2984a81e
commit bc04162a31
2 changed files with 51 additions and 0 deletions
+13
View File
@@ -174,6 +174,19 @@ function chatOpenDmWithUser(userId) {
}
function chatCsrfToken() {
// CSRF_COOKIE_HTTPONLY=True — cookie из JS не видна; как у HTMX берём из body hx-headers.
var body = document.body;
if (body) {
var hx = body.getAttribute('hx-headers');
if (hx) {
try {
var parsed = JSON.parse(hx);
if (parsed['X-CSRFToken']) return parsed['X-CSRFToken'];
} catch (e) { /* ignore */ }
}
}
var input = document.querySelector('input[name=csrfmiddlewaretoken]');
if (input && input.value) return input.value;
var m = document.cookie.match(/(?:^|; )csrftoken=([^;]*)/);
return m ? decodeURIComponent(m[1]) : '';
}