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

Update code for making Jquery and XSRF play nicely#197

Open
adithyaov wants to merge 1 commit intohaskell-servant:masterfrom
adithyaov:patch-2
Open

Update code for making Jquery and XSRF play nicely#197
adithyaov wants to merge 1 commit intohaskell-servant:masterfrom
adithyaov:patch-2

Conversation

@adithyaov
Copy link
Copy Markdown
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