You can add layout update in your custom module to add viewModel for catalog.topnav block in default.xml
file and also change template for your own instead of Magento_Theme::html/topmenu.phtml
, then in viewModel's class just use Magento\Customer\Model\Context
class instead of Magento\Customer\Model\Session
(and session's method isLoggedIn (it will not work because of depersonalization process)). Your viewModel can look like this:
namespace Vendor\Module\ViewModel;
class MyViewModel implements Magento\Framework\View\Element\Block\ArgumentInterface {
private $httpContext;
public function __construct(
\Magento\Framework\App\Http\Context $httpContext
) {
$this->httpContext = $httpContext;
}
public function getCustomerIsLoggedIn()
{
return (bool)$this->httpContext->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);
}
}
and in custom topmenu.phtml
template just wrap $_menuHtml
displaying in if statement with method from custom view model (getCustomerIsLoggedIn)