Files
zulip/static/js/csrf.js
Anders Kaseorg 6ec808b8df js: Add "use strict" directive to CommonJS files.
ES and TypeScript modules are strict by default and don’t need this
directive.  ESLint will remind us to add it to new CommonJS files and
remove it from ES and TypeScript modules.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2020-07-31 22:09:46 -07:00

27 lines
726 B
JavaScript

"use strict";
let csrf_token;
$(() => {
// This requires that we used Jinja2's {% csrf_input %} somewhere on the page.
const csrf_input = $('input[name="csrfmiddlewaretoken"]');
if (csrf_input.length > 0) {
csrf_token = csrf_input.attr("value");
} else {
csrf_token = undefined;
}
window.csrf_token = csrf_token;
if (csrf_token === undefined) {
return;
}
$.ajaxSetup({
beforeSend(xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", csrf_token);
}
},
});
});