How To

Magento 2: How to Get Admin User Data in Observer

Hey Magento Folks?,

How have you been? Embrace yourself at How-To Magento 2 Series. Today I will cover Magento 2 : How to get admin user data in the observer? with codes of it. Make sure you go through our previously published blog How to Change the Default Product Image Size in Magento 2. Let’s dive in?.

Introduction:

Generally, Magento offers creating events and observers. It is one type of Magento class where the created observer helps in identifying the performance, general behavior and to improve your business logic. Mainly, the observers are initiated whenever the events are created and implemented by the event manager. Here, I will help you to get admin user data in the observer.

Steps to get admin user data in observer:

Firstly, you need to create a separate extension or just inject the file in some other extension. both will work to get admin user data in the observer.

Step 1: Create event.xml in your extension at the below-given path:

Path: app\code\Vendor\Extension\etc\event.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="backend_auth_user_login_success">
        <observer name="backend_auth_user_login_success_observer" instance="Vendor\Extension\Observer\Backenduserobserver" />
    </event>
</config>

Step 2: After this, you need to create an observer file at the below path.

Path: app\code\Vendor\Extension\Observer\Backenduserobserver.php

<?php
namespace Vendor\Extension\Observer;

class Backenduserobserver implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $user = $observer->getEvent()->getUser();

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/ login_success.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($user->getData());

        return $this;
    }
}

That’s it this will provide you all the data while any user is login into the backend.

Final Words:

With assurance, the above solution will help you to get admin user data in observer in case any casualties do comment down your queries in the comment section below. Also, share the article to help your Magento developer friends via social media share. Till then Stay Safe Stay Healthy.

Happy Coding?

Click to rate this post!
[Total: 2 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

6 days ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

1 week ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

1 week ago

Top 10 Tips to Hire Shopify Developers

As the world of eCommerce continues to thrive, Shopify has become one of the most…

2 weeks ago

Managing Browser Events and Navigation in Shopify Remix: useBeforeUnload, useHref, and useLocation Hooks

Shopify Remix is an innovative framework that provides a streamlined experience for building fast, dynamic,…

2 weeks ago

Ultimate Guide to Hiring a Top Shopify Development Agency

Building a successful eCommerce store requires expertise, and for many businesses, Shopify has become the…

2 weeks ago