Reading list Switch to dark mode

    How to get Admin logged in status from frontend in Magento2

    Updated 22 February 2024

    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.

    Start your headless eCommerce
    now.
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Manup
  • Manoj Pal
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home