1

I'm working on implementing a SSO solution in Magento 2 for the admin side. Once the SSO authentication is complete I want to be able to log in the user from the frontend controller endpoint and redirect them to the admin dashboard.

Here is how I'm setting the auth session from the frontend controller action:

// I've verified this user model is valid and has a valid role.
$model = $this->userFactory->create()->load($sso_user['email'], 'email');
/** @var \Magento\Framework\App\State $appState */
$appState = ObjectManager::getInstance()->get('Magento\Framework\App\State');
$appState->emulateAreaCode('adminhtml', function ($model) {
  /** @var \Magento\Backend\Model\Auth\Session $session */
  $session = ObjectManager::getInstance()->get('Magento\Backend\Model\Auth\Session');
  $session->start();
  $session->setUser($model);
  $session->processLogin();
  // If I call `$session->isLoggedIn()` at this point it returns TRUE.
}, array($model));

Then I'm getting the $backend_url and it returns "adminhtml/dashboard"

$backend_url = '';
/** @var \Magento\Framework\App\State $appState */
$appState = ObjectManager::getInstance()->get('Magento\Framework\App\State');
$appState->emulateAreaCode('adminhtml', function () use (&$backend_url) {
  /** @var \Magento\Backend\Model\Url $url_model */
  $url_model = $this->_objectManager->get('Magento\Backend\Model\Url');
  $backend_url = $url_model->getStartupPageUrl();
}, array());

/** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath($backend_url);
return $resultRedirect;

However, once the redirects happens, it instead takes me to the admin login page. Am I not setting the admin user session correctly or is it something to do with attempting to redirect to a backend url from the frontend? Or something else I'm missing? Thanks for any help!

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.