Skip to content

Commit ce3eb13

Browse files
committed
Read CSRF cookie value from input field if cookie is absent
1 parent 59b410a commit ce3eb13

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/util.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,16 @@ export function getVerboseName(name) {
1919

2020

2121
export function getCsrfCookie() {
22-
if ((document.cookie.split(';').filter((item) => item.trim().indexOf('csrftoken=') === 0)).length) {
23-
return document.cookie.split(';').filter((item) => item.trim().indexOf('csrftoken=') === 0)[0].split('=')[1];
22+
let csrfCookies = document.cookie.split(';').filter((item) => item.trim().indexOf('csrftoken=') === 0);
23+
24+
if (csrfCookies.length) {
25+
return csrfCookies[0].split('=')[1];
26+
} else {
27+
// if no cookie found, get the value from the csrf form input
28+
let input = document.querySelector('input[name="csrfmiddlewaretoken"]');
29+
if (input)
30+
return input.value;
2431
}
32+
2533
return null;
2634
}

0 commit comments

Comments
 (0)