mirror of
https://github.com/symfony/recipes.git
synced 2026-09-11 15:16:30 +03:00
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:
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user