Use vanilla JS to get Rails CSRF values (#29403)

pull/29563/head
Matt Jankowski 2024-03-12 13:10:37 -04:00 committed by GitHub
parent d4ed7e466c
commit 00d94f3ffa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 6 deletions

View File

@ -1,5 +1,3 @@
import Rails from '@rails/ujs';
export const logOut = () => { export const logOut = () => {
const form = document.createElement('form'); const form = document.createElement('form');
@ -9,13 +7,18 @@ export const logOut = () => {
methodInput.setAttribute('type', 'hidden'); methodInput.setAttribute('type', 'hidden');
form.appendChild(methodInput); form.appendChild(methodInput);
const csrfToken = Rails.csrfToken(); const csrfToken = document.querySelector<HTMLMetaElement>(
const csrfParam = Rails.csrfParam(); 'meta[name=csrf-token]',
);
const csrfParam = document.querySelector<HTMLMetaElement>(
'meta[name=csrf-param]',
);
if (csrfParam && csrfToken) { if (csrfParam && csrfToken) {
const csrfInput = document.createElement('input'); const csrfInput = document.createElement('input');
csrfInput.setAttribute('name', csrfParam); csrfInput.setAttribute('name', csrfParam.content);
csrfInput.setAttribute('value', csrfToken); csrfInput.setAttribute('value', csrfToken.content);
csrfInput.setAttribute('type', 'hidden'); csrfInput.setAttribute('type', 'hidden');
form.appendChild(csrfInput); form.appendChild(csrfInput);
} }