Skip to content

Commit 194d485

Browse files
committed
ACP2E-1445: Customer data in Local Storage not reset when session file lost
- Fixed the issue.
1 parent 7377726 commit 194d485

File tree

6 files changed

+87
-71
lines changed

6 files changed

+87
-71
lines changed

app/code/Magento/Customer/Model/Cart/RemoveCookiesWhenQuoteIsNullPlugin.php

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\ViewModel\Customer;
9+
10+
use Magento\Customer\Model\Context;
11+
use Magento\Framework\App\Http\Context as HttpContext;
12+
use Magento\Framework\Serialize\Serializer\Json as Json;
13+
use Magento\Framework\View\Element\Block\ArgumentInterface;
14+
15+
/**
16+
* Customer's data view model
17+
*/
18+
class Data implements ArgumentInterface
19+
{
20+
/**
21+
* @var Json
22+
*/
23+
private $jsonEncoder;
24+
25+
/**
26+
*
27+
* @var HttpContext
28+
*/
29+
private $httpContext;
30+
31+
/**
32+
* @param HttpContext $httpContext
33+
* @param Json $jsonEncoder
34+
*/
35+
public function __construct(
36+
HttpContext $httpContext,
37+
Json $jsonEncoder
38+
) {
39+
$this->httpContext = $httpContext;
40+
$this->jsonEncoder = $jsonEncoder;
41+
}
42+
43+
/**
44+
* Check is user login
45+
*
46+
* @return bool
47+
*/
48+
public function isLoggedIn()
49+
{
50+
return $this->httpContext->getValue(Context::CONTEXT_AUTH);
51+
}
52+
53+
/**
54+
* Encode the mixed $valueToEncode into the JSON format
55+
*
56+
* @param mixed $valueToEncode
57+
* @return string
58+
*/
59+
public function jsonEncode($valueToEncode)
60+
{
61+
return $this->jsonEncoder->serialize($valueToEncode);
62+
}
63+
}

app/code/Magento/Customer/etc/frontend/di.xml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,6 @@
5757
<type name="Magento\Checkout\Block\Cart\Sidebar">
5858
<plugin name="customer_cart" type="Magento\Customer\Model\Cart\ConfigPlugin" />
5959
</type>
60-
<type name="Magento\Checkout\Block\Cart\Sidebar">
61-
<plugin name="customer_remove_items_from_cart" type="Magento\Customer\Model\Cart\RemoveCookiesWhenQuoteIsNullPlugin" />
62-
</type>
6360
<type name="Magento\Framework\Session\SessionManagerInterface">
6461
<plugin name="session_checker" type="Magento\Customer\CustomerData\Plugin\SessionChecker" />
6562
</type>

app/code/Magento/Customer/view/frontend/layout/default.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@
4848
</arguments>
4949
</block>
5050
<block name="customer.customer.data" class="Magento\Customer\Block\CustomerData"
51-
template="Magento_Customer::js/customer-data.phtml"/>
51+
template="Magento_Customer::js/customer-data.phtml">
52+
<arguments>
53+
<argument name="view_model" xsi:type="object">Magento\Customer\ViewModel\Customer\Data</argument>
54+
</arguments>
55+
</block>
5256
<block name="customer.data.invalidation.rules" class="Magento\Customer\Block\CustomerScopeData"
5357
template="Magento_Customer::js/customer-data/invalidation-rules.phtml"/>
5458
</referenceContainer>

app/code/Magento/Customer/view/frontend/templates/js/customer-data.phtml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
use Magento\Customer\ViewModel\Customer\Data;
7+
use Magento\Framework\App\ObjectManager;
68

79
/** @var \Magento\Customer\Block\CustomerData $block */
810

911
// phpcs:disable Magento2.Templates.ThisInTemplate.FoundHelper
12+
/** @var Data $viewModel */
13+
$viewModel = $block->getViewModel() ?? ObjectManager::getInstance()->get(Data::class);
14+
$customerDataUrl = $block->getCustomerDataUrl('customer/account/updateSession');
15+
$expirableSectionNames = $block->getExpirableSectionNames();
1016
?>
1117
<script type="text/x-magento-init">
1218
{
1319
"*": {
1420
"Magento_Customer/js/customer-data": {
1521
"sectionLoadUrl": "<?= $block->escapeJs($block->getCustomerDataUrl('customer/section/load')) ?>",
1622
"expirableSectionLifetime": <?= (int)$block->getExpirableSectionLifetime() ?>,
17-
"expirableSectionNames": <?= /* @noEscape */ $this->helper(\Magento\Framework\Json\Helper\Data::class)
18-
->jsonEncode($block->getExpirableSectionNames()) ?>,
23+
"expirableSectionNames": <?= /* @noEscape */ $viewModel->jsonEncode($expirableSectionNames) ?>,
1924
"cookieLifeTime": "<?= $block->escapeJs($block->getCookieLifeTime()) ?>",
20-
"updateSessionUrl": "<?= $block->escapeJs(
21-
$block->getCustomerDataUrl('customer/account/updateSession')
22-
) ?>"
25+
"updateSessionUrl": "<?= $block->escapeJs($customerDataUrl) ?>",
26+
"isLoggedIn": "<?= /* @noEscape */ $viewModel->isLoggedIn() ?>"
2327
}
2428
}
2529
}

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,20 @@ define([
4747
* Invalidate Cache By Close Cookie Session
4848
*/
4949
invalidateCacheByCloseCookieSession = function () {
50+
var isLoggedIn = parseInt(options.isLoggedIn, 10) || 0;
51+
5052
if (!$.cookieStorage.isSet('mage-cache-sessid')) {
5153
storage.removeAll();
5254
}
5355

56+
if (!$.localStorage.isSet('mage-customer-login')) {
57+
$.localStorage.set('mage-customer-login', isLoggedIn);
58+
}
59+
if ($.localStorage.get('mage-customer-login') !== isLoggedIn) {
60+
$.localStorage.set('mage-customer-login', isLoggedIn);
61+
storage.removeAll();
62+
}
63+
5464
$.cookieStorage.set('mage-cache-sessid', true);
5565
};
5666

0 commit comments

Comments
 (0)