Skip to content

Hide Menu If Customer is not Logged In. And Show when Login

I want to hide the menu when user is not logged in. And Show when It Logs in. I did the following code.

    public function __construct(
    MagentoCustomerModelSessionFactory $customerSession,
    LoggerInterface $logger,
    MagentoFrameworkAppResponseHttp $redirect,
    MagentoFrameworkAppHttpContext $httpContext
) {
    $this->customerSession = $customerSession;
    $this->logger = $logger;
    $this->redirect = $redirect;
    $this->httpContext = $httpContext;
}

public function getCustomerIsLoggedIn()
{
    if($this->customerSession->create()->isLoggedIn() == false)
    {
        $this->redirect->setRedirect('customer/account/login');
        $this->logger->info("Not Logged in");
    }
    else{
        return 1;
    }
}

Problem is It doesn’t show the menu after login. Only show when I clear cache. Same when I log out It doesn’t disappear. After cache clean It disappear. Please help.