0

I have observer of backend_auth_user_login_success.

I need the data of the admin user who is login in observer.

Can anyone please help me into this?

Thanks in advance.

1
  • By using \Magento\Backend\Model\Auth\Session $authSession you can get the current logged in admin user details without the observer. Ex: $this->authSession->getUser(); Commented May 29, 2020 at 16:47

1 Answer 1

2

You can create events.xml file here in your module

app/code/Vendor\Module/etc/events.xml

Content for this file is..

<?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\Module\Observer\BackendAuthObserver" />
    </event>
</config>

Now create Observer file here

app/code/Vendor/Module/Observer/BackendAuthObserver.php

<?php
namespace Vendor\Module\Observer;

class BackendAuthObserver 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/backend_auth_user_login_success.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($user->getData());

        return $this;
    }
}

Run below commands once

php bin/magento setup:upgrade
php bin/magento cache:clean

Now Go to your Backend and try to login with your username and password, and navigate to your var/log directory you can see backend_auth_user_login_success.log file there with your user content.

Hope this will help you!

2
  • 1
    Perfect answer. It helps lot. Thanks Commented May 29, 2020 at 18:16
  • @MayurSisodiya, Welcome Buddy. :-) Commented May 30, 2020 at 2:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.