fix: [LiveComponent] CSRF token not valid after form second submit

Fix the “invalid CSRF token” error when working with live-components and submitting a form a second time, for example if a server-side validation fails and the form is re-rendered.
This commit is contained in:
Marcos Gómez Vilches
2025-08-20 11:54:48 +02:00
committed by GitHub
parent b04a17d748
commit dd489df577
@@ -33,8 +33,8 @@ export function generateCsrfToken (formElement) {
if (!csrfCookie && nameCheck.test(csrfToken)) {
csrfField.setAttribute('data-csrf-protection-cookie-value', csrfCookie = csrfToken);
csrfField.defaultValue = csrfToken = btoa(String.fromCharCode.apply(null, (window.crypto || window.msCrypto).getRandomValues(new Uint8Array(18))));
csrfField.dispatchEvent(new Event('change', { bubbles: true }));
}
csrfField.dispatchEvent(new Event('change', { bubbles: true }));
if (csrfCookie && tokenCheck.test(csrfToken)) {
const cookie = csrfCookie + '_' + csrfToken + '=' + csrfCookie + '; path=/; samesite=strict';