Skip to content

Commit 661f8fa

Browse files
author
Patrick McLain
committed
Clear mage-cache-sessid on Ajax Login
This commit adds code based on `Magento\Customer\Controller\Account\LoginPost::execute` that clears the `mage-cache-sessid` cookie on login. This triggers a refresh of the customer data in localStorage. The primary purpose is to make sure the customer cart is properly loaded after login.
1 parent 5de0f07 commit 661f8fa

File tree

1 file changed

+24
-1
lines changed
  • app/code/Magento/Customer/Controller/Ajax

1 file changed

+24
-1
lines changed

app/code/Magento/Customer/Controller/Ajax/Login.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
1414
use Magento\Framework\App\Config\ScopeConfigInterface;
1515
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Stdlib\CookieManagerInterface;
17+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
1618

1719
/**
1820
* Login controller
@@ -58,6 +60,16 @@ class Login extends \Magento\Framework\App\Action\Action
5860
*/
5961
protected $scopeConfig;
6062

63+
/**
64+
* @var CookieManagerInterface
65+
*/
66+
protected $cookieManager;
67+
68+
/**
69+
* @var CookieMetadataFactory
70+
*/
71+
protected $cookieMetadataFactory;
72+
6173
/**
6274
* Initialize Login controller
6375
*
@@ -67,21 +79,27 @@ class Login extends \Magento\Framework\App\Action\Action
6779
* @param AccountManagementInterface $customerAccountManagement
6880
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
6981
* @param \Magento\Framework\Controller\Result\RawFactory $resultRawFactory
82+
* @param CookieManagerInterface $cookieManager
83+
* @param CookieMetadataFactory $cookieMetadataFactory
7084
*/
7185
public function __construct(
7286
\Magento\Framework\App\Action\Context $context,
7387
\Magento\Customer\Model\Session $customerSession,
7488
\Magento\Framework\Json\Helper\Data $helper,
7589
AccountManagementInterface $customerAccountManagement,
7690
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
77-
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory
91+
\Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
92+
CookieManagerInterface $cookieManager,
93+
CookieMetadataFactory $cookieMetadataFactory
7894
) {
7995
parent::__construct($context);
8096
$this->customerSession = $customerSession;
8197
$this->helper = $helper;
8298
$this->customerAccountManagement = $customerAccountManagement;
8399
$this->resultJsonFactory = $resultJsonFactory;
84100
$this->resultRawFactory = $resultRawFactory;
101+
$this->cookieManager = $cookieManager;
102+
$this->cookieMetadataFactory = $cookieMetadataFactory;
85103
}
86104

87105
/**
@@ -169,6 +187,11 @@ public function execute()
169187
$this->customerSession->setCustomerDataAsLoggedIn($customer);
170188
$this->customerSession->regenerateId();
171189
$redirectRoute = $this->getAccountRedirect()->getRedirectCookie();
190+
if ($this->cookieManager->getCookie('mage-cache-sessid')) {
191+
$metadata = $this->cookieMetadataFactory->createCookieMetadata();
192+
$metadata->setPath('/');
193+
$this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
194+
}
172195
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) {
173196
$response['redirectUrl'] = $this->_redirect->success($redirectRoute);
174197
$this->getAccountRedirect()->clearRedirectCookie();

0 commit comments

Comments
 (0)