Magento – Magento 2 get Admin session in frontend

adminmagento-2.1session

How can i get the admin session in frontend.

   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $adminSession = $objectManager->create('Magento\Security\Model\AdminSessionsManager');
   print_r($adminSession->getCurrentSession()->getStatus());

I tried with above script but no luck

Best Answer

protected $_session;

public function __construct(
    \Magento\Framework\Model\Context $context,
    \Magento\Backend\Model\Auth\Session $authSession
) {
    $this->_session = $authSession;

    parent::__construct($context);
}

and then call

$this->_session->isLoggedIn()

Note: don't use objectManager. Its not proper way to do it in Magento 2

Related Topic