diff --git a/app/code/Magento/Checkout/view/frontend/web/js/sidebar.js b/app/code/Magento/Checkout/view/frontend/web/js/sidebar.js index ee77efd18882fd6f6a62954908501251cab95645..afb34aa7826ab0bbeb09cbd08fdfcc889a160432 100644 --- a/app/code/Magento/Checkout/view/frontend/web/js/sidebar.js +++ b/app/code/Magento/Checkout/view/frontend/web/js/sidebar.js @@ -54,6 +54,8 @@ define([ customer = customerData.get('customer'); 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) { location.href = this.options.url.loginUrl; } else { diff --git a/app/code/Magento/Customer/Controller/Account/CreatePost.php b/app/code/Magento/Customer/Controller/Account/CreatePost.php index b332b69425637df8a735e7a0e9b5579e29d62138..1346ace778b8e557d82ff9e2ee994417a3772322 100644 --- a/app/code/Magento/Customer/Controller/Account/CreatePost.php +++ b/app/code/Magento/Customer/Controller/Account/CreatePost.php @@ -258,6 +258,12 @@ class CreatePost extends \Magento\Customer\Controller\AbstractAccount } else { $this->session->setCustomerDataAsLoggedIn($customer); $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(); } return $resultRedirect; diff --git a/app/code/Magento/Customer/Controller/Account/LoginPost.php b/app/code/Magento/Customer/Controller/Account/LoginPost.php index a65e81ab876b5e0a8990301fa02bac46653b9b32..a5ae36310fa22f456728f72e02d581c6cd165f9e 100644 --- a/app/code/Magento/Customer/Controller/Account/LoginPost.php +++ b/app/code/Magento/Customer/Controller/Account/LoginPost.php @@ -117,6 +117,14 @@ class LoginPost extends \Magento\Customer\Controller\AbstractAccount $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']); $this->session->setCustomerDataAsLoggedIn($customer); $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) { $value = $this->customerUrl->getEmailConfirmationUrl($login['username']); $message = __( diff --git a/app/code/Magento/Customer/Controller/Ajax/Login.php b/app/code/Magento/Customer/Controller/Ajax/Login.php index 4258ebf1ef0e18d1c4042425036a343860e8ab38..0c46ba76e52ba0c3c3631948ed68678872e03faa 100644 --- a/app/code/Magento/Customer/Controller/Ajax/Login.php +++ b/app/code/Magento/Customer/Controller/Ajax/Login.php @@ -9,6 +9,9 @@ namespace Magento\Customer\Controller\Ajax; use Magento\Customer\Api\AccountManagementInterface; use Magento\Framework\Exception\EmailNotConfirmedException; 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 @@ -43,6 +46,16 @@ class Login extends \Magento\Framework\App\Action\Action */ protected $resultRawFactory; + /** + * @var AccountRedirect + */ + protected $accountRedirect; + + /** + * @var ScopeConfigInterface + */ + protected $scopeConfig; + /** * Initialize Login controller * @@ -69,6 +82,55 @@ class Login extends \Magento\Framework\App\Action\Action $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. * @@ -103,6 +165,11 @@ class Login extends \Magento\Framework\App\Action\Action ); $this->customerSession->setCustomerDataAsLoggedIn($customer); $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) { $response = [ 'errors' => true, diff --git a/app/code/Magento/Customer/Model/Account/Redirect.php b/app/code/Magento/Customer/Model/Account/Redirect.php index ac134182e2aecaf18fa6b713234eaf0113bc9124..d3a74c51542909ada385e6d9dcf3a09989413ca4 100644 --- a/app/code/Magento/Customer/Model/Account/Redirect.php +++ b/app/code/Magento/Customer/Model/Account/Redirect.php @@ -16,12 +16,17 @@ use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Controller\Result\Redirect as ResultRedirect; use Magento\Framework\Controller\Result\Forward as ResultForward; use Magento\Framework\Url\DecoderInterface; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Stdlib\CookieManagerInterface; /** * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Redirect { + /** URL to redirect user on successful login or registration */ + CONST LOGIN_REDIRECT_URL = 'login_redirect'; + /** * @var RequestInterface */ @@ -57,6 +62,11 @@ class Redirect */ protected $resultFactory; + /** + * @var CookieManagerInterface + */ + protected $cookieManager; + /** * @param RequestInterface $request * @param Session $customerSession @@ -205,4 +215,60 @@ class Redirect { $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); + } } diff --git a/app/code/Magento/Customer/view/frontend/web/js/action/login.js b/app/code/Magento/Customer/view/frontend/web/js/action/login.js index 2cbc02ba07456e7526a83240cd8fd58d85700dd6..67bc734bcb7d6b050ba42f974579aab6e73b43c7 100644 --- a/app/code/Magento/Customer/view/frontend/web/js/action/login.js +++ b/app/code/Magento/Customer/view/frontend/web/js/action/login.js @@ -32,6 +32,8 @@ define( customerData.invalidate(['customer']); if (redirectUrl) { window.location.href = redirectUrl; + } else if (response.redirectUrl) { + window.location.href = response.redirectUrl; } else { location.reload(); }