Question

I am on Magento 2.3.1 and I am trying to login an admin user automatically given their username. I have tried using the Magento\Backend\Model\Auth\Session class like this:

$this->_authSession->setUser($adminUserName);
$this->_authSession->processLogin();
$this->_authSession->refreshAcl();
$this->_authSession->prolong();

and then redirecting like this:

return $this->_backendUrl->getUrl('admin/dashboard/index');

but it keeps giving me the login prompt instead of the dashboard even though this test:

var_dump($this->_authSession->isLoggedIn());

returns the boolean True.

Any help would be appreciated

Was it helpful?

Solution

This is what i managed to do (based on How to perform auto-login in Magento2 admin?) and is working

?php
require __DIR__ . '/app/bootstrap.php';

class TestApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface {

public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager
) {

    $this->_objectManager = $objectManager;
    $this->_eventManager = $this->_objectManager->get('Magento\Framework\Event\Manager');
    $this->_areaList = $this->_objectManager->get('Magento\Framework\App\AreaList');
    $this->_request = $this->_objectManager->get('Magento\Framework\App\Request\Http');
    $this->_response = $this->_objectManager->get('Magento\Framework\App\Response\Http');
    $this->_configLoader = $this->_objectManager->get('Magento\Framework\ObjectManager\ConfigLoaderInterface');
    $this->_state = $this->_objectManager->get('Magento\Framework\App\State');
    $this->_filesystem = $this->_objectManager->get('Magento\Framework\Filesystem');
    $this->registry = $this->_objectManager->get('\Magento\Framework\Registry');

    parent::__construct($objectManager, $this->_eventManager, $this->_areaList, $this->_request, $this->_response, $this->_configLoader, $this->_state, $this->_filesystem, $this->registry);


}

public function launch()
{
    $areaCode = 'adminhtml';
    $username = 'superadmin3';

    $this->_state->setAreaCode($areaCode);

    $this->_request->setPathInfo('/admin');

    $this->_objectManager->configure($this->_configLoader->load($areaCode));

    /** @var \Magento\User\Model\User $user */
    $user = $this->_objectManager->get('Magento\User\Model\User')->loadByUsername($username);

    /** @var \Magento\Backend\Model\Auth\Session $session */
    $session = $this->_objectManager->get('Magento\Backend\Model\Auth\Session');
    $session->setUser($user);
    $session->processLogin();

    if ($session->isLoggedIn()) {
        $cookieManager = $this->_objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
        $cookieValue = $session->getSessionId();
        if ($cookieValue) {
            $sessionConfig = $this->_objectManager->get('Magento\Backend\Model\Session\AdminConfig');
            $cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());
            $cookieMetadata = $this->_objectManager->get('Magento\Framework\Stdlib\Cookie\CookieMetadataFactory')
                ->createPublicCookieMetadata()
                ->setDuration(3600)
                ->setPath($cookiePath)
                ->setDomain($sessionConfig->getCookieDomain())
                ->setSecure($sessionConfig->getCookieSecure())
                ->setHttpOnly($sessionConfig->getCookieHttpOnly());
            $cookieManager->setPublicCookie($sessionConfig->getName(), $cookieValue, $cookieMetadata);
            /** @var \Magento\Security\Model\AdminSessionsManager $adminSessionManager */
            $adminSessionManager = $this->_objectManager->get('Magento\Security\Model\AdminSessionsManager');
            $adminSessionManager->processLogin();

        }

        /** @var \Magento\Backend\Model\UrlInterface $backendUrl */
        $backendUrl = $this->_objectManager->get('Magento\Backend\Model\UrlInterface');
        $path = $backendUrl->getStartupPageUrl();
        $url = $backendUrl->getUrl($path);
        $url = str_replace('autologin.php', 'index.php', $url);
        header('Location:  '.$url);
        exit;
    }
    return $this->_response;
}
}

$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('TestApp');
$bootstrap->run($app);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top