The Problem: Magento 2 Session Data Disappears on Google Chrome
Are you facing issues where your Magento 2 sessions data gets deleted when using Google Chrome? You’re not alone. This is a common problem that many developers encounter, especially after the recent changes in Chrome’s SameSite cookie behavior.
Things Already Explored
Before diving into solutions, let’s quickly recap what we know:
- The issue occurs only on Google Chrome.
- It happens when the user is redirected to an external payment gateway.
- Upon returning from the gateway, session data (including checkout sessions) is lost.
- From Chrome’s release notes (https://www.chromium.org/updates/same-site), we know that SameSite default value was changed to “Lax”. Disabling this does resolve the issue.
The Underlying Cause
The core problem lies in how Chrome handles cross-origin requests and cookies. When a request crosses an origin boundary (from your Magento site to the payment gateway), Chrome now enforces stricter security rules, especially with the SameSite attribute of cookies.
Why Does it Happen Only on Google Chrome?
Other browsers like Firefox and Safari haven’t adopted these changes yet or do so in different ways. Therefore, the issue remains specific to Chrome until other browsers align with its updated policy.
The Desired Solution
Your goal is clear: you want to ensure that all outgoing requests to third-party services have the SameSite attribute set to “None”. This will allow cross-origin cookies to be included in such requests, preserving your session data.
How to Implement SameSite=None for Outgoing Requests
To achieve this, we need to modify the way Magento initializes sessions and sends outgoing requests. Here’s a step-by-step approach:
-
Modify Magento Core Session Configuration
Open
app/code/Magento/Framework/App/Session/Namespace.php. Locate thegetSessionCookieParameters()method.
public function getSessionCookieParameters(): array
{
return [
'lifetime' => $this->cookieLifetime,
'path' => $this->sessionName,
'domain' => $this->cookieDomain,
'secure' => true,
'httponly' => true,
'samesite' => Cookie::SAMESITE_LAX, // Change this line
];
}
Change the 'samesite' value from Cookie::SAMESITE_LAX to Cookie::SAMESITE_NONE.
-
Override Outgoing Requests
For external API requests made by Magento (like for payment gateways), we need to manually include the session cookies in outgoing headers.
Create a custom module and override the following class:
Magento\Payment\Model\Info.php. In your custom model, modify thegetCcFormFields()method:
public function getCcFormFields()
{
$result = parent::getCcFormFields();
// Get the current session cookie value
$sessionCookie = $this->sessionManager->getSession()->getCookieParams()['cookie'];
// Add a custom header to include the session cookie in outgoing request
$result['additional_data'][] = [
'name' => 'Cookie',
'value' => $sessionCookie,
];
return $result;
}
-
Clear Magento Cache
After making these changes, flush your Magento cache (
bin/magento cache:clean).
Conclusion: Solving the Magento 2 Chrome Session Issue
By following this guide, you should now be able to preserve session data across all outgoing requests in Google Chrome. Remember that this is a specific solution for the current version of Chrome and may need adjustments as browser policies evolve.
Always keep up-to-date with the latest security practices and updates from your e-commerce platform provider. For more detailed guides on Magento 2 development, feel free to explore our resources at Laravel Company.
Happy coding!