0

I have a Sign in Link that successfully changes to sign out when a user is logged in, and successfully logs the user out when I click sign out. However, I ran into a bug. When Signed in, if I go to any other page or link, The Sign out link changes back to Sign in, Yet I show i am still signed in on the user dashboard.

Here is the code in my phtml file that switches the Sign in to Sign out.

<li class="nav-links">
<?php 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if($customerSession->isLoggedIn()) {
?>  <a href="<?php echo $this->getUrl("customer/account/logout");?>"><?php echo __("Sign Out");
} else {
?>  <a href="<?php echo $this->getUrl("customer/account/login");?>"><?php echo __("Sign 
In/Register");?></a>
<?php }
?>
</li>

Also, When clicking the hamburger menu to the right of the log out link when signed in, The hamburger menu logs me out. Here is the code for the menu button.

<button class="hamburger"><?php echo __("☰")?></button> <button class="cross">˟</button>

I am trying to get the logout button to stay visible no matter what page i visit after being signed in. There must be something i am not seeing here.

1 Answer 1

1

Magento pages are full page cached, so that's the problem with the Magento\Customer\Model\Session

For determining if user is logged use this class and run the method isLoggedIn ;)

use Magento\Customer\Model\Context;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\View\Element\Block\ArgumentInterface;

class Customer implements ArgumentInterface
{
    /** @var HttpContext */
    private $httpContext;

    /**
     * @param HttpContext $httpContext
     */
    public function __construct(
        HttpContext $httpContext
    ) {
        $this->httpContext = $httpContext;
    }

    /**
     * @return bool
     */
    public function isLoggedIn(): bool
    {
        return (bool)$this->httpContext->getValue(Context::CONTEXT_AUTH);
    }
}

Using object manager directly is considered a bad pratice.

Hope this helps you a bit.

@updated so it's a viewModel now.

3
  • where do I use that class? app/code/vendor/module/Helper?
    – Dylan T
    Sep 3, 2020 at 17:28
  • Hard to answer that, where do you need, but in general, yeah app/code/{vendor}/{module} will be the spot where the class should be placed, You can make it as a ViewModel, and then use it in your blocks, or inject this into your block class if you use one. I will upgrade the answer and make that the class is a ViewModel so you can then inject it easier into any block you need. Sep 3, 2020 at 19:06
  • ok sorry I am somewhat new to magento but it is making sense now. Thanks.
    – Dylan T
    Sep 4, 2020 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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