Observer to fire on customer login Url (baseUrl."customer/account/login") magento 2

by Hitesh Balpande   Last Updated July 11, 2019 11:09 AM

i want to fire observer on customer login 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");

    }


}


Answers 2


Try this

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class RedirectccpLogin implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "Customer LoggedIn";
        $customer = $observer->getEvent()->getCustomer();
        echo $customer->getName(); //Get customer name
        exit;
    }
}
MSA
MSA
July 11, 2019 10:46 AM

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.

Mohit Rane
Mohit Rane
July 11, 2019 10:48 AM

Related Questions


Redirect from Login Observer

Updated April 20, 2015 21:04 PM


magento 2 login observer (failed login)

Updated April 26, 2017 22:09 PM

CE1.9.1.1 - Custom logic injection in login event

Updated December 24, 2016 07:34 AM

magento1.9 get login event with observer

Updated September 11, 2017 14:09 PM