0

I'm aware, by default, Magento 2 provides us with a feature, that allows redirecting the customer to account dashboard after logging in, we can enable this feature by going to Stores → Settings → Configuration → Customers → Customer Configuration → Login Options. I have enabled that option to redirect the customer to account.

However, It's not working at all. I'm using Magento 2.3 EE. Is this feature was deprecated in Magento 2?

Could anyone please suggest me a way in redirecting the dashboard after login?

2

Use below extension to achieve this functionality

https://github.com/php-cuong/magento2-redirect-customer

Let me know if you need any further help.

Thanks in advance..!

0

Override the LoginPost class

<preference for="Magento\Customer\Controller\Account\LoginPost" type="Vendor\Module\Controller\Account\LoginPost" />

Vendor/Module/Controller/Account/LoginPost.php

<?php

namespace Vendor\Module\Controller\Account;

use Magento\Customer\Model\Account\Redirect as AccountRedirect;
use Magento\Framework\App\Action\Context;
use Magento\Customer\Model\Session;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\Exception\EmailNotConfirmedException;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Data\Form\FormKey\Validator;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class LoginPost extends \Magento\Customer\Controller\Account\LoginPost {

    public function execute() {
        if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath('home');
            return $resultRedirect;
        }

        if ($this->getRequest()->isPost()) {
            $login = $this->getRequest()->getPost('login');
            $resultRedirect = $this->resultRedirectFactory->create();

            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                    $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
                    $this->session->setCustomerDataAsLoggedIn($customer);
                    $this->session->regenerateId();
                } catch (EmailNotConfirmedException $e) {
                    $value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
                    $message = __(
                            'This account is not confirmed.' .
                            ' <a href="%1">Click here</a> to resend confirmation email.', $value
                    );
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);

                    $resultRedirect->setPath('yourpath');
                    return $resultRedirect;

                } catch (AuthenticationException $e) {
                    $message = __('Invalid login or password.');
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);

                    $resultRedirect->setPath('yourpath');
                    return $resultRedirect;

                } catch (\Exception $e) {
                    $this->messageManager->addError(__('Invalid login or password.'));
                    $resultRedirect->setPath('yourpath');
                    return $resultRedirect;
                }
            } else {
                $this->messageManager->addError(__('A login and a password are required.'));
                $resultRedirect->setPath('yourpath');
                return $resultRedirect;
            }
        }

        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('home');
        return $resultRedirect;
    }

}

$resultRedirect->setPath('yourpath'); add your custom path in this line mentioned in LoginPost.php file.

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.