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.
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!
\Magento\Backend\Model\Auth\Session
$authSession you can get the current logged in admin user details without the observer. Ex:$this->authSession->getUser();