Skip to content

Commit b7d6434

Browse files
authored
Merge pull request #1442 from algolia/bugfix/MAGE-787
Fix issue with cookies not cleared when customer logs out
2 parents 2b2c1d7 + e87f791 commit b7d6434

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

Helper/InsightsHelper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Algolia\AlgoliaSearch\Helper\Configuration\PersonalizationHelper;
66
use Algolia\AlgoliaSearch\Insights\UserInsightsClient;
77
use Algolia\AlgoliaSearch\InsightsClient;
8+
use Magento\Customer\Model\Customer;
89
use Magento\Customer\Model\Session as CustomerSession;
10+
use Magento\Framework\Session\SessionManagerInterface;
911
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
1012
use Magento\Framework\Stdlib\CookieManagerInterface;
1113

@@ -48,12 +50,14 @@ class InsightsHelper
4850
public function __construct(
4951
ConfigHelper $configHelper,
5052
PersonalizationHelper $personalizationHelper,
53+
SessionManagerInterface $sessionManager,
5154
CookieManagerInterface $cookieManager,
5255
CookieMetadataFactory $cookieMetadataFactory,
5356
CustomerSession $customerSession
5457
) {
5558
$this->configHelper = $configHelper;
5659
$this->personalizationHelper = $personalizationHelper;
60+
$this->sessionManager = $sessionManager;
5761
$this->cookieManager = $cookieManager;
5862
$this->cookieMetadataFactory = $cookieMetadataFactory;
5963
$this->customerSession = $customerSession;
@@ -142,11 +146,11 @@ private function getUserToken()
142146
}
143147

144148
/**
145-
* @param \Magento\Customer\Model\Customer $customer
149+
* @param Customer $customer
146150
*
147151
* @return string
148152
*/
149-
public function setUserToken(\Magento\Customer\Model\Customer $customer)
153+
public function setUserToken(Customer $customer)
150154
{
151155
$userToken = base64_encode('customer-' . $customer->getEmail() . '-' . $customer->getId());
152156
$userToken = 'aa-' . preg_replace('/[^A-Za-z0-9\-]/', '', $userToken);
@@ -166,4 +170,5 @@ public function setUserToken(\Magento\Customer\Model\Customer $customer)
166170

167171
return $userToken;
168172
}
173+
169174
}

Observer/Insights/CustomerLogout.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Observer\Insights;
4+
5+
use Algolia\AlgoliaSearch\Helper\InsightsHelper;
6+
use Magento\Framework\Event\ObserverInterface;
7+
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
8+
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
9+
10+
class CustomerLogout implements ObserverInterface
11+
{
12+
/**
13+
* @var PhpCookieManager
14+
*/
15+
private $cookieManager;
16+
17+
/**
18+
* @var CookieMetadataFactory
19+
*/
20+
private $cookieMetadataFactory;
21+
22+
/**
23+
* RefreshCustomerData constructor.
24+
* @param PhpCookieManager $cookieManager
25+
* @param CookieMetadataFactory $cookieMetadataFactory
26+
*/
27+
public function __construct(
28+
PhpCookieManager $cookieManager,
29+
CookieMetadataFactory $cookieMetadataFactory
30+
) {
31+
$this->cookieManager = $cookieManager;
32+
$this->cookieMetadataFactory = $cookieMetadataFactory;
33+
}
34+
35+
/**
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)
41+
*/
42+
public function execute(\Magento\Framework\Event\Observer $observer)
43+
{
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);
48+
}
49+
}
50+
}

etc/frontend/events.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@
2323
<event name="checkout_onepage_controller_success_action">
2424
<observer name="algoliasearch_insights_place_order_event" instance="Algolia\AlgoliaSearch\Observer\Insights\CheckoutOnepageControllerSuccessAction" />
2525
</event>
26+
<event name="customer_logout">
27+
<observer name="algoliasearch_personalization_unset_user_token" instance="Algolia\AlgoliaSearch\Observer\Insights\CustomerLogout" />
28+
</event>
29+
<event name="persistent_session_expired">
30+
<observer name="refresh_customer_data" instance="Algolia\AlgoliaSearch\Observer\Insights\CustomerLogout" />
31+
</event>
2632
</config>

view/frontend/web/insights.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ define(
3737
initializeAnalytics: function () {
3838
algoliaAnalytics.init({
3939
appId: this.config.applicationId,
40-
apiKey: this.config.apiKey
40+
apiKey: this.config.apiKey,
41+
useCookie: true,
4142
});
4243

4344
var userAgent = 'insights-js-in-magento (' + this.config.extensionVersion + ')';

0 commit comments

Comments
 (0)