The "Invalid form key" error in Magento is usually caused by a mismatch between the form key stored in the session and the one submitted with the form. This can happen due to a number of reasons, such as a stale form key in the session, an expired session, or a misconfiguration of Magento's security settings.
Here are some possible solutions you can try:
Clear your browser's cache and cookies, and try again. This will ensure that you have the latest form key from Magento.
Check if your web server's clock is synchronized with the correct time. If the clock is out of sync, it can cause issues with Magento's session management and form key generation.
Make sure that your Magento installation is configured correctly with the correct base URL and secure URLs. You can check this in your Magento Admin Panel under Stores > Configuration > General > Web.
Check if your server has the correct permissions to write to the Magento var/ and generated/ directories. If these directories are not writable, it can cause issues with Magento's session management and form key generation.
If you are using a caching plugin or CDN, try disabling it temporarily to see if it resolves the issue. Sometimes, caching can interfere with Magento's form key generation and validation.
Finally, check if your Magento installation is up-to-date with the latest security patches and updates. Vulnerabilities in older versions of Magento can sometimes lead to issues with session management and form key generation.