0

Now I implement the code below

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Http\Context as AuthContext;

class Customer extends AbstractHelper
{
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    private $httpContext;
    private $objectmanager;
    private $authContext;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Framework\ObjectManagerInterface $objectmanager,
        Session $session,
        AuthContext $authContext
    ) {
        $this->objectmanager = $objectmanager;
        $this->httpContext = $httpContext;
        $this->customerSession = $session;
        $this->authContext = $authContext;

        parent::__construct($context);        
    }

    public function isLoggedIn()
    {
        $isLoggedIn = $this->authContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

        return $isLoggedIn;   
    }

But when I refresh my page it still say that it's login eventhough I already logged out. Below is the code of my template

<?php
    $customerHelper = $this->helper('Vendor\Module\Helpers\Customer');
?>

<ul class="menu_list">
<?php if ($customerHelper->isLoggedIn()): ?>
    <li>                        
        <a href="<?= $this->getUrl('customer/account'); ?>"><?= $block->escapeHtmlAttr(__('Account')) ?></a>
    </li>
    <li>
        <a href="<?= $this->getUrl('customer/account/logout'); ?>"><?= $block->escapeHtmlAttr(__('Logout')) ?></a>
    </li>
<?php else: ?>
    <li>
        <a href="<?= $this->getUrl('customer/account/login'); ?>"><?= $block->escapeHtmlAttr(__('Login')) ?></a>
    </li>
<?php endif; ?>             
</ul>

Update: - When I logout the account still thinks that I'm still login. When checking the function response it says that I'm still logged in. I'm not sure how to fix this. Upon checking the session when I'm logged out there are no values of firstname for the mage_cache_storage. But when I'm loggedin its being populated. I think a Javascript is populating this value that's why the PHP script still thinks I'm logged in.. I'm not sure how to fix this

0

Maybe try a different approach

https://github.com/DominicWatts/CustomerLogin/blob/master/view/frontend/templates/index/index.phtml#L2-L11

    <!-- ko if: customer().is_logged_in  -->
    <span class="logged-in"
            data-bind="text: new String('<?= $block->escapeHtml(__('Welcome back %1', '%1')) ?>').replace('%1', customer().firstname)">
    </span>
    <!-- /ko -->
    <!-- ko ifnot: customer().is_logged_in  -->
    <span class="not-logged-in"
            data-bind='html:"<?= $block->escapeHtml(__("Please <a href='%1'>login</a>", $block->getUrl('customer/account/login'))) ?>"'>
    </span>
    <!-- /ko -->

Is_logged_in comes from the plugin

https://github.com/DominicWatts/CustomerLogin/blob/master/Plugin/Magento/Customer/CustomerData/Customer.php#L43

Dont forget data-bind="scope: 'customer'

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.