Skip to content
This repository has been archived by the owner on Oct 29, 2021. It is now read-only.

Update code for making Jquery and XSRF play nicely #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

adithyaov
Copy link
Contributor

@adithyaov adithyaov commented Sep 7, 2021

We need to dynamically find and set the header on every request.

I did not check for jquery but in the case of htmx:

The following does not work for multiple consecutive requests,

var token = (function() {
    r = document.cookie.match(new RegExp('XSRF-TOKEN=([^;]+)'))
    if (r) return r[1];
})();

document.body.addEventListener('htmx:configRequest', (event) => {
  event.detail.headers['X-XSRF-TOKEN'] = token;
});

The following does,

var getXSRFToken = function() {
    r = document.cookie.match(new RegExp('XSRF-TOKEN=([^;]+)'))
    if (r) return r[1];
};

document.body.addEventListener('htmx:configRequest', (event) => {
  event.detail.headers['X-XSRF-TOKEN'] = getXSRFToken();
});

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant