0

I'm trying to check if the user is logged in. But the problem is in my code eventhough I already logged out it's still check and output that the user is still logged in. Below is my code

<?php
namespace Tcm\Goodgifts\Helper;

class Customer extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Http\Context $httpContext
    ) {
        $this->customerSession = $customerSession;
        $this->context = $httpContext;
        parent::__construct($context);
    }

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

        var_dump($this->customerSession);

        return $this->customerSession->isLoggedIn();
    }
}

So basically if I run the bin/magento setup:upgrade the correct output is being displayed. But after I log out or login it retains the old state and not the new one. When I outputted the value of $this->context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH) it give me this

enter image description here enter image description here

So as you can see it somehow get the value under _httpContext->data["customer_logged_in"] but when checking the value I think the correct value should be the one under the _httpContext->default["customer_logged_in"] but I don't know how to get this value.

Just by running $this->context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH) will retrieve the object Data and not the Default.

Any idea how to get this value? I'm guessing the Default note returns the correct/updated value

0

You sort of half answered your question

When fullpage cache enable generic session class will not work as you expected

https://magento.stackexchange.com/a/279788/70343

3
  • Ok so what will be he solution? Aside from disabling the fullpage cache which is not an option for me right now Feb 24 '20 at 11:54
  • @MadzQuestioning did you see this magento.stackexchange.com/a/305070/70343 Feb 24 '20 at 12:53
  • Yeah this actually solved my problem. Although I had trouble thinking about how to bind the Customer since I don't know Knockout js. But when I looked at the header.phtml of magento theme I was able to fix it. Thanks for the help Feb 24 '20 at 16:08

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.