0

I have used two plugins to use the referral url's for customer login page.

here is the code of my etc/di.xml

  <type name="Magento\Customer\Controller\Account\LoginPost">
        <plugin name="conditional_redirect_after_login" type="Vendor\Module\Plugin\Customer\LoginPost" sortOrder="1" disabled="false" />
    </type>

    <type name="Magento\Customer\Controller\Account\Login">
        <plugin name="set_redirect_url" type="Vendor\Module\Plugin\Customer\LoginCheck" sortOrder="1" disabled="false" />
    </type>

Code of LoginPost.php

 <?php
 namespace Vendor\Module\Plugin\Customer;
 use Magento\Customer\Model\Session;
 use Magento\Framework\Data\Form\FormKey\Validator;
 use Magento\Customer\Api\CustomerRepositoryInterface;
 use Magento\Framework\Message\ManagerInterface;
 use Magento\Framework\App\Response\Http as ResponseHttp;
 use Magento\Framework\App\Config\ScopeConfigInterface as ScopeConfig;
 use Magento\Framework\UrlInterface;
 use Magento\Customer\Model\Account\Redirect as AccountRedirect;
class LoginPost
{
protected $session;
/** @var Validator */
protected $formKeyValidator;
/** @var CustomerRepositoryInterface */
protected $customerRepositoryInterface;
/** @var ManagerInterface **/
protected $messageManager;
protected $_objectManager;
protected $_connector;
/** @var Http **/
protected $responseHttp;
protected $currentCustomer;
protected $_customer; 
protected $_storeManager;    
protected $resultFactory;
protected $redirect;
protected $accountRedirect;
protected $resultRedirectFactory;

public function __construct(
    Session $customerSession,
    Validator $formKeyValidator,
    CustomerRepositoryInterface $customerRepositoryInterface,
    ManagerInterface $messageManager,
    \Inkind\Navision\Model\Connector $connector,
    ResponseHttp $responseHttp,
    ScopeConfig $scopeConfig,
    \Magento\Customer\Model\CustomerFactory $customer,
    UrlInterface $url,
    \Magento\Framework\ObjectManagerInterface $objectmanager,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\Controller\ResultFactory $resultFactory,
    \Magento\Framework\App\Response\RedirectInterface $redirect,
    AccountRedirect $accountRedirect,
    \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory
)
{
    $this->scopeConfig = $scopeConfig;
    $this->session = $customerSession;
    $this->formKeyValidator = $formKeyValidator;
    $this->customerRepositoryInterface = $customerRepositoryInterface;
    $this->messageManager = $messageManager;
    $this->responseHttp = $responseHttp;
    $this->_customer = $customer; 
    $this->_url = $url;
    $this->_connector = $connector;
    $this->_objectManager = $objectmanager;
    $this->_storeManager = $storeManager; 
    $this->resultFactory = $resultFactory;
    $this->redirect = $redirect;
    $this->accountRedirect = $accountRedirect;
    $this->resultRedirectFactory       = $resultRedirectFactory;
}
public function afterExecute(\Magento\Customer\Controller\Account\LoginPost $subject, $result) 
{       
    $storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;        
    if ($subject->getRequest()->isPost()) {
        $login = $subject->getRequest()->getPost('login');

        if (!empty($login['username']) && !empty($login['password'])) {

            $customerId = $this->getCustomer(trim($login['username']));
            if(isset($customerId) && $customerId!= ''){
                $currentCustomer = $this->getcurrentcustomer($customerId);

                $status = $currentCustomer->getCustomerActivated(); 
                $configValue = $this->scopeConfig->getValue('customer/startup/redirect_dashboard', $storeScope);    
                try {
                    if($status == 0){                       
                        $message = "This account is awaiting activation.";
                        $this->messageManager->addError($message);
                        $this->session->logout();                   
                        $result->setPath('customer/account/login');

                        return $result;                      
                    }else{                                      
                        if ($configValue == 1) {
                            $result->setPath('customer/account');   
                        } else {
                            $redirectUrl = $this->session->getBeforeLoginUrl();
                            $this->session->unsBeforeLoginUrl();
                            $result->setPath($redirectUrl);                                                 
                        }   

                        return $result;
                    }
                }
                catch (\Exception $e)
                {
                    $message = "Invalid user Credentials.";
                    $this->messageManager->addError($e->getMessage());
                    $this->session->setUsername($login['username']);
                    $this->responseHttp->setRedirect('customer/account/login');

                }
            }else{
                $message = "The email address was not recognised!";
                $this->messageManager->addError($message);
                $result->setPath('customer/account/login');                         
                return $result;                             
            }

        }            
    }

}

/**
 * @param $email
 * @return \Magento\Customer\Api\Data\CustomerInterface
 */
public function getCustomer($email)
{
    try{
        $websiteID = $this->_storeManager->getStore()->getWebsiteId();
        $customer = $this->_customer->create()->setWebsiteId($websiteID)->loadByEmail($email);
        $userId = $customer->getId();           
        return $userId;
    }catch (\Exception $e){

        return false;
    }
}

public function getcurrentcustomer($id){
    $customerObj = $this->_customer->create();
    $customer = $customerObj->load($id);
    return $customer; 
}

public function getStoreId()
{
    return $this->_storeManager->getStore()->getId();
}
public function getWebsiteId(){
   return $this->_storeManager->getStore()->getWebsiteId();
   }
 }

Code of LoginCheck.php

class LoginCheck {
protected $redirect;
protected $session;

public function __construct(
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Customer\Model\Session $customerSession
) {
    $this->redirect = $redirect;
    $this->session = $customerSession;
}

public function beforeExecute(
\Magento\Customer\Controller\Account\Login $subject
) {
    $redirectUrl = $this->redirect->getRefererUrl();
    $this->session->setBeforeLoginUrl($redirectUrl);
    }
  }

This is working as expected, but i still want to redirect to login page if customer is not logged in with correct password.

Right now it is redirecting to referral url if not logged in correctly. Anyone please check it and help me . Thanks

  • Have you check log file? – Dhiren Vasoya Dec 3 '18 at 14:17
  • @DhirenVasoya, there is no error in log, i think its not any error related , we need to check the conditions. – jafar pinjar Dec 3 '18 at 14:23
  • @DhirenVasoya, could you check how to redirect to login page if logged in with incorrect password? – jafar pinjar Dec 3 '18 at 15:38
  • @DhirenVasoya, have you checked it? – jafar pinjar Dec 5 '18 at 8:08
  • Do you want to like that whenever customer add username and password and execute login button and before that you want to check customer id pw right? – Rohan Hapani Dec 5 '18 at 9:26

Your Answer

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

Browse other questions tagged or ask your own question.