Here we learn, how to know the admin logged in status from frontend in Magento2.
Write the below code where ever you need, to check admin logged in or not.
/** * check if admin logged in or not * @return boolean */ public function isAdminloggedIn() { $dateTime = $this->_objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime'); $adminConfig = $this->_objectManager->create('Magento\Security\Model\Config'); $lifetime = $adminConfig->getAdminSessionLifetime(); $currentTime = $dateTime->gmtTimestamp(); $adminSession = $this->_objectManager->create('Magento\Security\Model\AdminSessionsManager'); $lastUpdatedTime = $dateTime->gmtTimestamp($adminSession->getCurrentSession()->getUpdatedAt()); if (!is_numeric($lastUpdatedTime)) { $lastUpdatedTime = strtotime($lastUpdatedTime); } if ($lastUpdatedTime >= ($currentTime - $lifetime) && $adminSession->getCurrentSession()->getStatus() == 1 ) { return true; } else { return false; } }
That’s it.