1
0
mirror of synced 2025-12-19 18:10:59 -05:00
Files
docs/javascripts/get-csrf.js
Kevin Heis c450d8d555 Send CSRF tokens over XHR (#15778)
* Send CSRF tokens over XHR

* Update events.js

* Update browser.js
2020-09-28 09:44:14 -07:00

21 lines
576 B
JavaScript

export async function fillCsrf () {
const response = await fetch('/csrf', {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
})
const json = response.ok ? await response.json() : {}
const meta = document.createElement('meta')
meta.setAttribute('name', 'csrf-token')
meta.setAttribute('content', json.token)
document.querySelector('head').append(meta)
}
export default function getCsrf () {
const csrfEl = document
.querySelector('meta[name="csrf-token"]')
if (!csrfEl) return ''
return csrfEl.getAttribute('content')
}