Sahil
Sahil

Reputation: 67

How to get loggedin user session in magento?

I created a module,that show list of user . I want to show that page if user is logged-in admin panel

 // world.php



<?php
namespace Pulsestorm\HelloWorldMVVM\Controller\Hello;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;
class World extends \Magento\Framework\App\Action\Action
{
    protected $pageFactory;
    public function __construct(Context $context, PageFactory $pageFactory)
    {
        $this->pageFactory = $pageFactory;
        return parent::__construct($context);
    }
    public function execute()
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        var_dump("feererggrerge",$customerSession->isLoggedIn());
        var_dump(__METHOD__);
        $page_object = $this->pageFactory->create();;
        return $page_object;
    }
}

Directory structure

I want to get admin session in my module controller to check if he is logged in admin panel

Upvotes: 0

Views: 551

Answers (1)

Sukhjinder Singh
Sukhjinder Singh

Reputation: 489

You can check user logged in or not using this

Method 1. To check the customer is logged or not on any page of the website:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerSession = $objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) {

   // customer login action

}

Method 2. To check if customer is logged from controller:

$this->_objectManager->get('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) {

   // customer login action

}

<?php 
    if(!Mage::getSingleton('customer/session')->isLoggedIn()){
        //not logged in
    }else{
        // logged in
    }
?>

Upvotes: 1

Related Questions