servant-auth icon indicating copy to clipboard operation
servant-auth copied to clipboard

Update code for making Jquery and XSRF play nicely

Open adithyaov opened this issue 4 years ago • 0 comments
trafficstars

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();
});

adithyaov avatar Sep 07 '21 14:09 adithyaov