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.
public function isAdminloggedIn() { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $dateTime = $objectManager->create('Magento\Framework\Stdlib\DateTime\DateTime'); $adminConfig = $objectManager->create('Magento\Security\Model\Config'); $lifetime = $adminConfig->getAdminSessionLifetime(); $currentTime = $dateTime->gmtTimestamp(); $adminSession = $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 && $adminSession->getCurrentSession()->getUserId() ) { return true; } else { return false; } }
That’s it.
2 comments