Skip to content

Commit 34c72fd

Browse files
committed
MAGE-891: add Observer to refresh cookies
1 parent bbc54f6 commit 34c72fd

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

Helper/InsightsHelper.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,30 @@ public function setAuthenticatedUserToken(Customer $customer): string|null
157157
return $userToken;
158158
}
159159

160+
/**
161+
* Sets a token for a non-authenticated user
162+
*
163+
* @return void
164+
*/
165+
public function setNonAuthenticatedUserToken(): void
166+
{
167+
$metaData = $this->cookieMetadataFactory->createPublicCookieMetadata()
168+
->setDuration($this->configHelper->getCookieLifetime())
169+
->setPath('/')
170+
->setHttpOnly(false)
171+
->setSecure(false);
172+
173+
try {
174+
$this->cookieManager->setPublicCookie(
175+
InsightsHelper::ALGOLIA_ANON_USER_TOKEN_COOKIE_NAME,
176+
(string) $this->cookieManager->getCookie(InsightsHelper::ALGOLIA_ANON_USER_TOKEN_COOKIE_NAME),
177+
$metaData
178+
);
179+
} catch (LocalizedException $e) {
180+
$this->logger->error("Error writing anonymous user cookie: " . $e->getMessage());
181+
}
182+
}
183+
160184
/**
161185
* @param int|null $storeId
162186
* @return bool
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
} else {
36+
$this->insightsHelper->setNonAuthenticatedUserToken();
37+
}
38+
}
39+
}

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)