Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento : How to check if admin is logged in within a module controller?

I'm creating a Magento Module. Within the controller, I want to check if an admin is logged in or not. So the controller only will be accessible if there is a logged in admin.

I'm trying to use this code on my controller.

Mage::getSingleton('core/session', array('name' => 'adminhtml')); 
$session = Mage::getSingleton('admin/session');

// Use the 'admin/session' object to check loggedIn status
if ( $session->isLoggedIn() ) {
   echo "logged in";
} else {
   echo "not logged in";
}

but I always get "not logged in", even if I'm already logged in to the magento admin.

Can anybody help me to resolve this issue?? any help will be much appreciated. Thanks

like image 519
Calua Avatar asked Jul 27 '10 09:07

Calua


People also ask

How do you check if customer is logged in or not in Magento 2?

{ ... $this->_session = $session; ... } Then you need to use Magento\Customer\Model\Session::isLoggedIn() to check if the customer is logged in or not.


1 Answers

That is really strange. I use almost exactly the same code and it works all the time:

//get the admin session
Mage::getSingleton('core/session', array('name'=>'adminhtml'));

//verify if the user is logged in to the backend
if(Mage::getSingleton('admin/session')->isLoggedIn()){
  //do stuff
}
else
{
  echo "go away bad boy";
}

Did you try var_dumping the $session variable? Maybe it will help you get on the right track...

like image 166
silvo Avatar answered Oct 13 '22 19:10

silvo