Skip to content

Commit 48ce0d7

Browse files
authored
Merge pull request #1574 from algolia/feature/MAGE-891/token-expiration
MAGE-891: Handle tokens expiration
2 parents 9dca4d3 + 93e8a85 commit 48ce0d7

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

Helper/ConfigHelper.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class ConfigHelper
8181
public const CC_ANALYTICS_IS_SELECTOR = 'algoliasearch_cc_analytics/cc_analytics_group/is_selector';
8282
public const CC_CONVERSION_ANALYTICS_MODE = 'algoliasearch_cc_analytics/cc_analytics_group/conversion_analytics_mode';
8383
public const CC_ADD_TO_CART_SELECTOR = 'algoliasearch_cc_analytics/cc_analytics_group/add_to_cart_selector';
84+
public const COOKIE_LIFETIME = 'web/cookie/cookie_lifetime';
8485

8586
public const GA_ENABLE = 'algoliasearch_analytics/analytics_group/enable';
8687
public const GA_DELAY = 'algoliasearch_analytics/analytics_group/delay';
@@ -142,7 +143,7 @@ class ConfigHelper
142143
protected const IS_LOOKING_SIMILAR_ENABLED_IN_PDP = 'algoliasearch_recommend/recommend/looking_similar/is_looking_similar_enabled_on_pdp';
143144
protected const IS_LOOKING_SIMILAR_ENABLED_IN_SHOPPING_CART = 'algoliasearch_recommend/recommend/looking_similar/is_looking_similar_enabled_on_cart_page';
144145
protected const LOOKING_SIMILAR_TITLE = 'algoliasearch_recommend/recommend/looking_similar/title';
145-
public const LEGACY_USE_VIRTUAL_REPLICA_ENABLED = 'algoliasearch_instant/instant/use_virtual_replica';
146+
public const LEGACY_USE_VIRTUAL_REPLICA_ENABLED = 'algoliasearch_instant/instant/use_virtual_replica';
146147
protected const AUTOCOMPLETE_KEYBORAD_NAVIAGATION = 'algoliasearch_autocomplete/autocomplete/navigator';
147148
protected const FREQUENTLY_BOUGHT_TOGETHER_TITLE = 'algoliasearch_recommend/recommend/frequently_bought_together/title';
148149
protected const RELATED_PRODUCTS_TITLE = 'algoliasearch_recommend/recommend/related_product/title';
@@ -1872,4 +1873,13 @@ public function isCookieRestrictionModeEnabled()
18721873
{
18731874
return (bool)$this->cookieHelper->isCookieRestrictionModeEnabled();
18741875
}
1876+
1877+
/**
1878+
* @param $storeId
1879+
* @return mixed
1880+
*/
1881+
public function getCookieLifetime($storeId = null)
1882+
{
1883+
return $this->configInterface->getValue(self::COOKIE_LIFETIME, ScopeInterface::SCOPE_STORE, $storeId);
1884+
}
18751885
}

Helper/InsightsHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function setAuthenticatedUserToken(Customer $customer): string|null
142142
$userToken = $this->generateAuthenticatedUserToken($customer);
143143

144144
$metaData = $this->cookieMetadataFactory->createPublicCookieMetadata()
145-
->setDurationOneYear()
145+
->setDuration($this->configHelper->getCookieLifetime())
146146
->setPath('/')
147147
->setHttpOnly(false)
148148
->setSecure(false);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Observer\Insights;
4+
5+
use Algolia\AlgoliaSearch\Helper\InsightsHelper;
6+
use Magento\Customer\Model\Session as CustomerSession;
7+
use Magento\Framework\Event\Observer;
8+
use Magento\Framework\Event\ObserverInterface;
9+
10+
class CookieRefresherObserver implements ObserverInterface
11+
{
12+
/**
13+
* CookieRefresherObserver observer constructor.
14+
*
15+
* @param CustomerSession $customerSession
16+
* @param InsightsHelper $insightsHelper
17+
*
18+
*/
19+
public function __construct(
20+
private readonly CustomerSession $customerSession,
21+
private readonly InsightsHelper $insightsHelper,
22+
) {}
23+
24+
/**
25+
* Renew anonymous or customer session token to update the lifetime
26+
*
27+
* @param Observer $observer
28+
*
29+
* @return void
30+
*/
31+
public function execute(Observer $observer): void
32+
{
33+
if ($this->customerSession->isLoggedIn()) {
34+
$this->insightsHelper->setAuthenticatedUserToken($this->customerSession->getCustomer());
35+
}
36+
}
37+
}

Observer/Insights/CustomerLogout.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function execute(\Magento\Framework\Event\Observer $observer): void
4747
try {
4848
$this->cookieManager->setPublicCookie(self::UNSET_AUTHENTICATION_USER_TOKEN_COOKIE_NAME, 1, $metaDataUnset);
4949
$this->cookieManager->deleteCookie(InsightsHelper::ALGOLIA_CUSTOMER_USER_TOKEN_COOKIE_NAME, $metadata);
50+
$this->cookieManager->deleteCookie(InsightsHelper::ALGOLIA_ANON_USER_TOKEN_COOKIE_NAME, $metadata);
5051
} catch (LocalizedException $e) {
5152
$this->logger->error("Error writing Algolia customer cookies: " . $e->getMessage());
5253
}

etc/frontend/events.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@
3232
<event name="catalog_controller_product_init_after">
3333
<observer name="algoliasearch_current_product" instance="Algolia\AlgoliaSearch\Observer\RegisterCurrentProductObserver"/>
3434
</event>
35+
<event name="controller_action_predispatch">
36+
<observer name="algoliasearch_cookie_refresher" instance="Algolia\AlgoliaSearch\Observer\Insights\CookieRefresherObserver" shared="false" />
37+
</event>
3538
</config>

0 commit comments

Comments
 (0)