0
 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('/shop/');  //Here  i changed but not working 
        return $resultRedirect;
    }

    if ($this->getRequest()->isPost()) {
        $login = $this->getRequest()->getPost('login');
        if (!empty($login['username']) && !empty($login['password'])) {
            try {
                $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
                $this->session->setCustomerDataAsLoggedIn($customer);
                $this->session->regenerateId();
                if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
                    $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
                    $metadata->setPath('');
                    $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
                }
                $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 = __(
                    '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']);
            } catch (UserLockedException $e) {
                $message = __(
                    'You did not sign in correctly or your account is temporarily disabled.'
                );
                $this->messageManager->addError($message);
                $this->session->setUsername($login['username']);
            } catch (AuthenticationException $e) {
                $message = __('You did not sign in correctly or your account is temporarily disabled.');
                $this->messageManager->addError($message);
                $this->session->setUsername($login['username']);
            } catch (LocalizedException $e) {
                $message = $e->getMessage();
                $this->messageManager->addError($message);
                $this->session->setUsername($login['username']);
            } catch (\Exception $e) {
                // PA DSS violation: throwing or logging an exception here can disclose customer password
                $this->messageManager->addError(
                    __('An unspecified error occurred. Please contact us for assistance.')
                );
            }
        } else {
            $this->messageManager->addError(__('A login and a password are required.'));
        }
    }

    return $this->accountRedirect->getRedirect();
}

}

Customer/controller/account/loginpost.php

if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */

$resultRedirect = $this->resultRedirectFactory->create();

$resultRedirect->setPath('/shop/'); **//Here I changed, but not working** 

How I can solve this ???

any help

  • You doing wrong... please check the condition. You have tried to redirect to shop page when you have already logged IN or Your form key is already invalid – Amit Bera Feb 17 at 7:25
  • where the path i can change – prabhakaran7 Feb 17 at 7:37
0

At mageto2, Magento\Customer\Model\Account\Redirect:getRedirect() is reposirbale for redirection after login.

from this method, Magento set prepare redirection URL, So, if you want change Magento default to redirect logic, then you have to work at this method.

In my option, best to use around (aroundGetRedirect()) plugin over getRedirect()

Plugin Class:

<?php
namespace Devamitbera\CustomerRedirection\Plugin;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\UrlInterface;


class Redirect
{

    private $url;

    private $resultFactory;
    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    private $request;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        UrlInterface $url, 
        ResultFactory $resultFactory)
    {

        $this->url = $url;
        $this->resultFactory = $resultFactory;
        $this->request = $request;
    }

    public function aroundGetRedirect ($subject, \Closure $proceed)
    {

        $allowActions = ['loginpost','createpost'];
        // Customer  redirection when   createpost and loginpost hit 
        if(in_array(strtolower($this->request->getActionName()),$allowActions)){
                    $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
            $result->setUrl($this->url->getUrl('shop));
            return $result;
        }

        return $proceed();
    }
}

Create di.ml at app/code/Devamitbera/CustomerRedirection/etc/

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <type name="Magento\Customer\Model\Account\Redirect">
        <plugin name="NewAcctRedirectPlug" type="Devamitbera\CustomerRedirection\Plugin\Redirect" disabled="false" sortOrder="1"/>
    </type>
</config>
  • where i can in di.xml file – prabhakaran7 Feb 17 at 14:20

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.