Skip to content
Snippets Groups Projects
Commit 8d51d9e5 authored by Serhiy Shkolyarenko's avatar Serhiy Shkolyarenko
Browse files

MAGETWO-49692: Customer is not redirected to checkout on login if guest is disabled

added URL processing to login and registration controllers
parent bc1338aa
Branches
No related merge requests found
...@@ -54,6 +54,8 @@ define([ ...@@ -54,6 +54,8 @@ define([
customer = customerData.get('customer'); customer = customerData.get('customer');
if (!customer().firstname && !cart().isGuestCheckoutAllowed) { if (!customer().firstname && !cart().isGuestCheckoutAllowed) {
// set URL for redirect on successful login/registration. It's postprocessed on backend.
$.cookie('login_redirect', this.options.url.checkout);
if (this.options.url.isRedirectRequired) { if (this.options.url.isRedirectRequired) {
location.href = this.options.url.loginUrl; location.href = this.options.url.loginUrl;
} else { } else {
......
...@@ -258,6 +258,12 @@ class CreatePost extends \Magento\Customer\Controller\AbstractAccount ...@@ -258,6 +258,12 @@ class CreatePost extends \Magento\Customer\Controller\AbstractAccount
} else { } else {
$this->session->setCustomerDataAsLoggedIn($customer); $this->session->setCustomerDataAsLoggedIn($customer);
$this->messageManager->addSuccess($this->getSuccessMessage()); $this->messageManager->addSuccess($this->getSuccessMessage());
$requestedRedirect = $this->accountRedirect->getRedirectCookie();
if (!$this->scopeConfig->getValue('customer/startup/redirect_dashboard') && $requestedRedirect) {
$resultRedirect->setUrl($this->_redirect->success($requestedRedirect));
$this->accountRedirect->clearRedirectCookie();
return $resultRedirect;
}
$resultRedirect = $this->accountRedirect->getRedirect(); $resultRedirect = $this->accountRedirect->getRedirect();
} }
return $resultRedirect; return $resultRedirect;
......
...@@ -117,6 +117,14 @@ class LoginPost extends \Magento\Customer\Controller\AbstractAccount ...@@ -117,6 +117,14 @@ class LoginPost extends \Magento\Customer\Controller\AbstractAccount
$customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']); $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
$this->session->setCustomerDataAsLoggedIn($customer); $this->session->setCustomerDataAsLoggedIn($customer);
$this->session->regenerateId(); $this->session->regenerateId();
$redirectUrl = $this->accountRedirect->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {
$this->accountRedirect->clearRedirectCookie();
$resultRedirect = $this->resultRedirectFactory->create();
// URL is checked to be internal in $this->_redirect->success()
$resultRedirect->setUrl($this->_redirect->success($redirectUrl));
return $resultRedirect;
}
} catch (EmailNotConfirmedException $e) { } catch (EmailNotConfirmedException $e) {
$value = $this->customerUrl->getEmailConfirmationUrl($login['username']); $value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
$message = __( $message = __(
......
...@@ -9,6 +9,9 @@ namespace Magento\Customer\Controller\Ajax; ...@@ -9,6 +9,9 @@ namespace Magento\Customer\Controller\Ajax;
use Magento\Customer\Api\AccountManagementInterface; use Magento\Customer\Api\AccountManagementInterface;
use Magento\Framework\Exception\EmailNotConfirmedException; use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\Framework\Exception\InvalidEmailOrPasswordException; use Magento\Framework\Exception\InvalidEmailOrPasswordException;
use Magento\Framework\App\ObjectManager;
use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Magento\Framework\App\Config\ScopeConfigInterface;
/** /**
* Login controller * Login controller
...@@ -43,6 +46,16 @@ class Login extends \Magento\Framework\App\Action\Action ...@@ -43,6 +46,16 @@ class Login extends \Magento\Framework\App\Action\Action
*/ */
protected $resultRawFactory; protected $resultRawFactory;
/**
* @var AccountRedirect
*/
protected $accountRedirect;
/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
/** /**
* Initialize Login controller * Initialize Login controller
* *
...@@ -69,6 +82,55 @@ class Login extends \Magento\Framework\App\Action\Action ...@@ -69,6 +82,55 @@ class Login extends \Magento\Framework\App\Action\Action
$this->resultRawFactory = $resultRawFactory; $this->resultRawFactory = $resultRawFactory;
} }
/**
* Get account redirect.
* For release backward compatibility.
*
* @deprecated
* @return AccountRedirect
*/
protected function getAccountRedirect()
{
if (!is_object($this->accountRedirect)) {
$this->accountRedirect = ObjectManager::getInstance()->get(AccountRedirect::class);
}
return $this->accountRedirect;
}
/**
* Account redirect setter for unit tests.
*
* @deprecated
* @param AccountRedirect $value
* @return void
*/
public function setAccountRedirect($value)
{
$this->accountRedirect = $value;
}
/**
* @deprecated
* @return ScopeConfigInterface
*/
protected function getScopeConfig()
{
if (!is_object($this->scopeConfig)) {
$this->scopeConfig = ObjectManager::getInstance()->get(ScopeConfigInterface::class);
}
return $this->scopeConfig;
}
/**
* @deprecated
* @param ScopeConfigInterface $value
* @return void
*/
public function setScopeConfig($value)
{
$this->scopeConfig = $value;
}
/** /**
* Login registered users and initiate a session. * Login registered users and initiate a session.
* *
...@@ -103,6 +165,11 @@ class Login extends \Magento\Framework\App\Action\Action ...@@ -103,6 +165,11 @@ class Login extends \Magento\Framework\App\Action\Action
); );
$this->customerSession->setCustomerDataAsLoggedIn($customer); $this->customerSession->setCustomerDataAsLoggedIn($customer);
$this->customerSession->regenerateId(); $this->customerSession->regenerateId();
$redirectRoute = $this->getAccountRedirect()->getRedirectCookie();
if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectRoute) {
$response['redirectUrl'] = $this->_redirect->success($redirectRoute);
$this->getAccountRedirect()->clearRedirectCookie();
}
} catch (EmailNotConfirmedException $e) { } catch (EmailNotConfirmedException $e) {
$response = [ $response = [
'errors' => true, 'errors' => true,
......
...@@ -16,12 +16,17 @@ use Magento\Framework\App\Config\ScopeConfigInterface; ...@@ -16,12 +16,17 @@ use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Controller\Result\Redirect as ResultRedirect; use Magento\Framework\Controller\Result\Redirect as ResultRedirect;
use Magento\Framework\Controller\Result\Forward as ResultForward; use Magento\Framework\Controller\Result\Forward as ResultForward;
use Magento\Framework\Url\DecoderInterface; use Magento\Framework\Url\DecoderInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\CookieManagerInterface;
/** /**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/ */
class Redirect class Redirect
{ {
/** URL to redirect user on successful login or registration */
CONST LOGIN_REDIRECT_URL = 'login_redirect';
/** /**
* @var RequestInterface * @var RequestInterface
*/ */
...@@ -57,6 +62,11 @@ class Redirect ...@@ -57,6 +62,11 @@ class Redirect
*/ */
protected $resultFactory; protected $resultFactory;
/**
* @var CookieManagerInterface
*/
protected $cookieManager;
/** /**
* @param RequestInterface $request * @param RequestInterface $request
* @param Session $customerSession * @param Session $customerSession
...@@ -205,4 +215,60 @@ class Redirect ...@@ -205,4 +215,60 @@ class Redirect
{ {
$this->session->setBeforeAuthUrl($url); $this->session->setBeforeAuthUrl($url);
} }
/**
* Get Cookie manager. For release backward compatibility.
*
* @deprecated
* @return CookieManagerInterface
*/
protected function getCookieManager()
{
if (!is_object($this->cookieManager)) {
$this->cookieManager = ObjectManager::getInstance()->get(CookieManagerInterface::class);
}
return $this->cookieManager;
}
/**
* Set cookie manager. For unit tests.
*
* @deprecated
* @param $value
*/
public function setCookieManager($value)
{
$this->cookieManager = $value;
}
/**
* Get redirect route from cookie for case of successful login/registration
*
* @return null|string
*/
public function getRedirectCookie()
{
return $this->getCookieManager()->getCookie(self::LOGIN_REDIRECT_URL, null);
}
/**
* Save redirect route to cookie for case of successful login/registration
*
* @param string $route
* @return void
*/
public function setRedirectCookie($route)
{
$this->getCookieManager()->setPublicCookie(self::LOGIN_REDIRECT_URL, $route);
}
/**
* Clear cookie with requested route
*
* @return void
*/
public function clearRedirectCookie()
{
$this->getCookieManager()->deleteCookie(self::LOGIN_REDIRECT_URL);
}
} }
...@@ -32,6 +32,8 @@ define( ...@@ -32,6 +32,8 @@ define(
customerData.invalidate(['customer']); customerData.invalidate(['customer']);
if (redirectUrl) { if (redirectUrl) {
window.location.href = redirectUrl; window.location.href = redirectUrl;
} else if (response.redirectUrl) {
window.location.href = response.redirectUrl;
} else { } else {
location.reload(); location.reload();
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment