#Getting 419s despite including X-CSRF-TOKEN headers

3 messages · Page 1 of 1 (latest)

manic grail
#

I'm using an Inertia F/E so Blade directives aren't an option but when I grab the cookie with

export const getCookie = (name = 'XSRF-TOKEN') => {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length === 2) return parts.pop().split(';').shift();
};

for POST and DELETE routes to attach to a header with

 const response = await fetch(url, {
      method,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        'X-CSRF-TOKEN': getCookie()
      },

it fails, I've also tried X-XSRF-TOKEN just to be thorough but that wasn't any better. Previously I had been simply including a _token plucked from React state but that's been a nightmare to keep in sync and I'd like to move toward something a bit more inline with how things are supposed to work

sick hound
#

You might need to decodeURIComponent the cookie value

sick hound
#

Yeah, axios does that

    read: function read(name) {
      var match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)'));
      return match ? decodeURIComponent(match[3]) : null;
    },