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:
<?php
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() {
$this->_request->setPathInfo('/admin');
//SOME CODE TO GET THE USER DATA FROM SSO SERVER
$user = $this->_objectManager->get('Magento\User\Model\User')->loadByUsername($userSsoObject['email']);
$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($session->getName(), $cookieValue, $cookieMetadata);
if (class_exists('Magento\Security\Model\AdminSessionsManager')) {
/** @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;
}
}
}
}
Any suggestions as the why it's not working?
$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.