Skip to content

Commit 41f73e7

Browse files
committed
MAGE-787 Fixed issue with cookies not cleared when customer logs out
1 parent 2afed95 commit 41f73e7

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

Helper/InsightsHelper.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,4 @@ public function setUserToken(Customer $customer)
171171
return $userToken;
172172
}
173173

174-
/**
175-
* @param Customer $customer
176-
* @return void
177-
*/
178-
public function unsetUserToken(Customer $customer)
179-
{
180-
if ($this->cookieManager->getCookie(self::ALGOLIA_CUSTOMER_USER_TOKEN_COOKIE_NAME)) {
181-
$metadata = $this->cookieMetadataFactory->createCookieMetadata()
182-
->setPath($this->sessionManager->getCookiePath());
183-
return $this->cookieManager->deleteCookie(
184-
self::ALGOLIA_CUSTOMER_USER_TOKEN_COOKIE_NAME,$metadata);
185-
}
186-
}
187174
}

Observer/Insights/CustomerLogout.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,48 @@
33
namespace Algolia\AlgoliaSearch\Observer\Insights;
44

55
use Algolia\AlgoliaSearch\Helper\InsightsHelper;
6-
use Magento\Customer\Model\Customer;
7-
use Magento\Framework\Event\Observer;
86
use Magento\Framework\Event\ObserverInterface;
7+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
8+
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
99

1010
class CustomerLogout implements ObserverInterface
1111
{
12+
/**
13+
* @var PhpCookieManager
14+
*/
15+
private $cookieManager;
1216

13-
/** @var InsightsHelper */
14-
protected $insightsHelper;
17+
/**
18+
* @var CookieMetadataFactory
19+
*/
20+
private $cookieMetadataFactory;
1521

1622
/**
17-
* @param InsightsHelper $insightsHelper
23+
* RefreshCustomerData constructor.
24+
* @param PhpCookieManager $cookieManager
25+
* @param CookieMetadataFactory $cookieMetadataFactory
1826
*/
1927
public function __construct(
20-
InsightsHelper $insightsHelper
21-
)
22-
{
23-
$this->insightsHelper = $insightsHelper;
28+
PhpCookieManager $cookieManager,
29+
CookieMetadataFactory $cookieMetadataFactory
30+
) {
31+
$this->cookieManager = $cookieManager;
32+
$this->cookieMetadataFactory = $cookieMetadataFactory;
2433
}
2534

2635
/**
27-
* @param Observer $observer
28-
* ['customer' => $customer]
36+
* Check and clear session data if persistent session expired
37+
*
38+
* @param \Magento\Framework\Event\Observer $observer
39+
* @return void
40+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
2941
*/
30-
public function execute(Observer $observer)
42+
public function execute(\Magento\Framework\Event\Observer $observer)
3143
{
32-
/** @var Customer $customer */
33-
$customer = $observer->getEvent()->getCustomer();
34-
35-
if ($this->insightsHelper->getConfigHelper()->isClickConversionAnalyticsEnabled($customer->getStoreId()) || $this->insightsHelper->getPersonalizationHelper()->isPersoEnabled($customer->getStoreId())) {
36-
$this->insightsHelper->unsetUserToken($customer);
44+
if ($this->cookieManager->getCookie(InsightsHelper::ALGOLIA_CUSTOMER_USER_TOKEN_COOKIE_NAME)) {
45+
$metadata = $this->cookieMetadataFactory->createCookieMetadata();
46+
$metadata->setPath('/');
47+
$this->cookieManager->deleteCookie(InsightsHelper::ALGOLIA_CUSTOMER_USER_TOKEN_COOKIE_NAME, $metadata);
3748
}
3849
}
39-
}
50+
}

etc/frontend/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@
2626
<event name="customer_logout">
2727
<observer name="algoliasearch_personalization_unset_user_token" instance="Algolia\AlgoliaSearch\Observer\Insights\CustomerLogout" />
2828
</event>
29+
<event name="persistent_session_expired">
30+
<observer name="refresh_customer_data" instance="Algolia\AlgoliaSearch\Observer\Insights\CustomerLogout" />
31+
</event>
2932
</config>

0 commit comments

Comments
 (0)