here is the code:
events.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="my_custom_login_success_observer" instance="Example\CatIntegration\Observer\AdminLoginSucceeded" />
</event>
</config>
AdminLoginSucceeded.php
<?php
namespace Example\CatIntegration\Observer;
use Magento\Framework\Event\ObserverInterface;
class AdminLoginSucceeded implements ObserverInterface
{
private $resultRedirectFactory;
protected $_storeManager;
protected $responseFactory;
private $url;
protected $adminSession;
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
\Magento\Framework\UrlInterface $url,
\Magento\Backend\Model\Auth\Session $adminSession
){
$this->_storeManager = $storeManager;
$this->url = $url;
$this->responseFactory = $responseFactory;
$this->resultRedirectFactory = $responseFactory;
$this->adminSession = $adminSession;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$event = $observer->getEvent();
$roleData = $this->adminSession->getUser()->getRole()->getData();
//if (strpos($roleData['role_name'], 'test') !== false) {
$RedirectUrl= $this->url->getUrl('example/customcat/index');
$this->responseFactory->create()->setRedirect($RedirectUrl)->sendResponse();
/*$writer = new \Zend_Log_Writer_Stream(BP . '/var/log/AdminLoginSucceeded.log');
$logger = new \Zend_Log();
$logger->addWriter($writer);
$logger->info($roleData['role_name']);*/
die();
//}
}
}
the above code works in http, but when i put it in https production env, it show me
Invalid security or form key. Please refresh the page.