cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 How to check if Admin Logged on frontend

SOLVED

Magento 2 How to check if Admin Logged on frontend

In Magento 1 below code works

Mage::getSingleton('core/session', array('name' => 'adminhtml'));
$adminSession = Mage::getSingleton('admin/session');
if ($adminSession->isLoggedIn()) {
    $adminIpAddress = $adminSession['_session_validator_data']['remote_addr'];
}

 

I am trying below code in magento 2 that is working in admin section but not working on frontend

$adminSession= $this->auth->isLoggedIn();

including (\Magento\Backend\Model\Auth)

 

I need to check if admin is logged in or not on front end in magento2

 

Any help will be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 How to check if Admin Logged on frontend

What I found in my quick research is:


In magento 1 it was quite easy to get the admin session on frontend since magento had no restrictions when it expose the admin session cookie.

 

But in magento2, the admin cookie will get exposed only for the admin path, for example /admin, because of this, you won't be able to access the admin session on frontend.

 

But as an alternate solution, you can try:

  1. Build an admin route & a controller for you module
  2. When frontend loads, send an ajax request to the admin controller and check the auth for isLoggedIn()
  3. Initialize your plugin / related code via JS through ajax callback.

 

View solution in original post

1 REPLY 1

Re: Magento 2 How to check if Admin Logged on frontend

What I found in my quick research is:


In magento 1 it was quite easy to get the admin session on frontend since magento had no restrictions when it expose the admin session cookie.

 

But in magento2, the admin cookie will get exposed only for the admin path, for example /admin, because of this, you won't be able to access the admin session on frontend.

 

But as an alternate solution, you can try:

  1. Build an admin route & a controller for you module
  2. When frontend loads, send an ajax request to the admin controller and check the auth for isLoggedIn()
  3. Initialize your plugin / related code via JS through ajax callback.