I'm trying to create a module which logs in a retrieved user to the administration panel. (we're talking admins, not customers :) ) I found the following post on how to do this: How to perform auto-login in Magento2 admin?

The problem with my module is that it doesn't seem to be able to log in. Once I've retrieved the user data the sign in code processes without errors and I'm redirected to the admin panel login screen without being logged in. Besides that there is an addition setAreaCode function. However whenever I try to use that Magento tells me the area code is already set (to frontend as that's where the route is coming from).

I have the following code:


namespace Adwise\Authwise\Controller\Index;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\State;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\Result\PageFactory;

class Index extends \Magento\Framework\App\Action\Action {

    protected $pageFactory;

    protected $objectManager;

     * @var
    private $_state;

    public function __construct(Context $context, PageFactory $pageFactory, ObjectManagerInterface $objectManager, State $state) {
        $this->pageFactory = $pageFactory;
        $this->objectManager = $objectManager;
        $this->helper = $this->objectManager->create('Adwise\Authwise\Helper\Data');
        $this->_state = $state;

        return parent::__construct($context);

    public function execute() {



                $user = $this->_objectManager->get('Magento\User\Model\User')->loadByUsername($userSsoObject['email']);

                $session = $this->_objectManager->get('Magento\Backend\Model\Auth\Session');

                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')
                        $cookieManager->setPublicCookie($session->getName(), $cookieValue, $cookieMetadata);
                        if (class_exists('Magento\Security\Model\AdminSess‌​ionsManager')) {
                            /** @var \Magento\Security\Model\AdminSessionsManager $adminSessionManager */
                            $adminSessionManager = $this->_objectManager->get('Magento\Security\Model\AdminSess‌​ionsManager');

                    /** @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);

                return $this->_response;


Any suggestions as the why it's not working?

  • The redirect URL that is returned is actually: magento.dev/admin/authwise/index/denied/key/… There does seem to be a session set but it appears to be invalid. Non of the session setting methods return any error though. Commented May 24, 2017 at 9:09
  • Emulating the areacode adminhtml using the state class also doesn't seem to change a thing. magento-quickies.alanstorm.com/post/143553447080/… Commented May 24, 2017 at 11:56
  • I'm at a complete loss here. If I print: $adminSessionManager = $this->_objectManager->get('Magento\Security\Model\AdminSessionsManager'); $adminSessionManager->processLogin(); die($adminSessionManager->getCurrentSession()->isLoggedInStatus()); the users shows logged in yet for some reason I still keep getting the login screen. Commented May 24, 2017 at 12:40
  • The denied redirect mentioned in my first comment was actually change to a redirect link to the dashboard by adding the areacode emulator but the log-in screen is still displayed :( Any pointers would be appreciated Commented May 26, 2017 at 7:41

1 Answer 1


Ok so basically the solution consisted of two parts. First of all I had to use the emulate adminhtml function from the state class. After that I was completely baffled by the fact that the sessions were being set and everything pointed to the fact that I was logged in. After comparing the sessions the session name turned out to be PHPSID in stead of the 'admin' which it should be. Changing the session name actually solved my problem.

  • Can you please share what changes you actually made? I am also doing something similar.
    – Gagan
    Commented Feb 21, 2018 at 7:27
  • I am stuck on setareacode function. It says Area code is already set. I am in magento 2.2.2
    – Gagan
    Commented Feb 21, 2018 at 8:29
  • Please try this link : magento.stackexchange.com/a/367579/82670
    – Msquare
    Commented Jun 7, 2023 at 22:52

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.