2

I want to fire observer on customer login Page url(baseUrl."customer/account/login") magento 2.

I tried customer_login event but it's firing after customer login.

etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="customer_login">
        <observer name="redirect_to_ccp_login" instance="Vendor\Module\Observer\RedirectccpLogin" />
    </event>
</config>

Vendor\Module\Observer\RedirectccpLogin.php

<?php 
namespace Vendor\Module\Observer;
use Magento\Framework\Controller\ResultFactory;
class RedirectccpLogin implements \Magento\Framework\Event\ObserverInterface 
{
    protected $_logger;
    protected $resultFactory;
    protected $_responseFactory;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
         \Psr\Log\LoggerInterface $logger,
         \Magento\Framework\Controller\ResultFactory $resultFactory,
         \Magento\Framework\App\ResponseFactory $responseFactory
    ) {
        $this->_logger = $logger;
        $this->resultFactory = $resultFactory;
        $this->_responseFactory = $responseFactory;
    }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $this->_logger->info("ccp customer login fire");

    }


}
  • Can you try changing your classname Redirectccplogin instead of RedirectccpLogin, i know that's silly but someone got solved just because of this. – Mohit Rane Jul 11 at 11:14
1

I overrided Magento\Customer\Controller\Account\Login Controller

Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

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

</config>

Vendor\Module\Controller\Customer\Login.php

<?php

namespace Vendor\Module\Controller\Customer;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Customer\Controller\AbstractAccount;
use Magento\Customer\Model\Session;
use Magento\Framework\Controller\ResultFactory;

class Login extends \Magento\Customer\Controller\Account\Login
{
      /**
     * @var Session
     */
    protected $session;
    protected $_logger;
    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Context $context
     * @param Session $customerSession
     * @param PageFactory $resultPageFactory
     */
    protected $resultFactory;
    protected $_responseFactory;

    protected $_storeManager;

    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
         Session $customerSession,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Controller\ResultFactory $resultFactory,
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_responseFactory = $responseFactory;
        $this->resultPageFactory = $resultPageFactory;
        $this->_logger = $logger;
        $this->session = $customerSession;
        $this->_storeManager = $storeManager;
        parent::__construct($context,$customerSession,$resultPageFactory);
    }

    /**
     * Customer login form page
     *
     * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
     */
    public function execute()
    {

       $this->_logger->info("cpp customer login  using controller.");

    }
}

Above code worked for me

0

You can try using Plugin,

Create a di.xml file app/code/Vendor/Module/etc/di.xml file

<?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\Controller\Account\LoginPost">
        <plugin name="customer_login_plugin" type="Vendor\Module\Plugin\Customer\LoginPost" sortOrder="10" disabled="false"/>
    </type>
</config>

Create a Plugin file app/code/Vendor/Module/Plugin/Customer/LoginPost.php

<?php

namespace Vendor\Module\Plugin\Customer;

class LoginPost
{
    public function __construct(
        \Magento\Framework\App\Action\Context $context
    ) {
        $this->_request = $context->getRequest();
        $this->_response = $context->getResponse();
    }

    public function aroundExecute(\Magento\Customer\Controller\Account\LoginPost $subject, $proceed)
    {           
        $login =  $this->_request->getPost('login');     

        // before login check condition

        $returnValue = $proceed();            

        // after login check condition

        return $returnValue;
    }
}

Hope it helps.

  • this plugin also working after customer login time only but i want to redirect to some other url once we hit our magento baseUrl.(“customer/account/login”). – Hitesh Balpande Jul 11 at 11:05
  • i changed class RedirectccpLogin to Redirectccplogin but no result – Hitesh Balpande Jul 11 at 11:29

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.