Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 2 session data gets deleted in google chrome

Problem:
When my magento2.3 application redirects user to payment gateway, i can access all the session data. but when it returns backs from there it do not have checkout session data or any session data. this happens only for google chrome

Things i already explored
From google chrome release notes (https://www.chromium.org/updates/same-site) i can see they have changed samesite default value to "Lax", and disabling this works.

Solution Looking for
I want to give samesite=None value to all my outgoing requests to any third party services. Any help or lead would be highly appreciated.

like image 500
Ajay Avatar asked Aug 28 '20 14:08

Ajay


2 Answers

You can try setting the samesite=None by following steps..

file : etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\View\Element\Js\Cookie">
        <plugin name="afterGetPath" type="namespace\module\Plugin\View\Element\Js\ManagePath" sortOrder="10"/>
    </type>
</config>

file : Plugin/View/Element/Js/ManagePath.php

namespace namespace\module\Plugin\View\Element\Js;

use Magento\Framework\View\Element\Js\Cookie;

class ManagePath
{
    public function afterGetPath(\Magento\Framework\View\Element\Js\Cookie $subject, $path)
    {
        
        if (preg_match('/SameSite/', $path)) {
             $path_array = explode(';', $path);
             $path = $path_array[0];
        }
        
        return $path;
    }
}

file : etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <preference for="Magento\Framework\Session\Config\ConfigInterface" type="namespace\module\Session\CustomConfig"/>
</config>

file : Session/CustomConfig.php


namespace namespace\module\Session;

use Magento\Framework\Session\Config as DefaultConfig;

class CustomConfig extends DefaultConfig
{
    public function setCookiePath($path, $default = null)
    {   
        parent::setCookiePath($path, $default);

        $path = $this->getCookiePath();

        //check and update path of cookie
        if (!preg_match('/SameSite/', $path)) {
            $path .= '; SameSite=None';
            $this->setOption('session.cookie_path', $path);
        }

        return $this;
    }
}

NOTE : replace namespace & module with your namespace and module.

like image 73
Sanjay Sharma Avatar answered Sep 20 '22 20:09

Sanjay Sharma


Since I don't have enough reputation to comment on the accepted answer, I must point out that for me it didn't work since Chrome asked that all cookies with SameSite set to "none" to be flagged as secure. The fix was adding:

$path .= '; SameSite=None ; secure';

Without flagging them as secure I would have problems adding items to cart.

Worked for me, hope it helps others that encounter same issue.

like image 37
Mircea Andrei Avatar answered Sep 23 '22 20:09

Mircea Andrei