Question

I need to detect in frontend section for my custom module if customer has just successfully logged in, and i need to run some function before it redirect to another page, is there any way to do this?

Was it helpful?

Solution

Use customer_login event observer for customer login action

1) Create events.xml

app/code/Vendor/Module/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="customer_login_observer" instance="Vendor\Module\Observer\CustomerLogin" />
    </event>
</config>

2) Now create observer CustomerLogin.php

app/code/Vendor/Module/Observer/CustomerLogin.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerLogin implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "Customer LoggedIn";
        $customer = $observer->getEvent()->getCustomer();
        echo $customer->getName(); //Get customer name
        exit;
    }
}

OTHER TIPS

Use to try below code..

Magento 2.3 not allowed to use CustomerLogin this kind of formats in observer class so kindly use Customerlogin instead-of CustomerLogin class that would be work.

<?php

   namespace Vendor\Module\Observer;

   use Magento\Framework\Event\ObserverInterface;

   class Customerlogin implements ObserverInterface
   {        

   public function execute(\Magento\Framework\Event\Observer $observer)
   {   
    $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/customerdata.log');
    $logger = new \Zend\Log\Logger();
    $logger->addWriter($writer);

    $customer = $observer->getEvent()->getCustomer();
    echo $customer->getName(); //Get customer name
    $logger->info(print_r($customer->getEmail(),true));
    die();
    }
  } 

This is working well for me..

Happy Coding!!

Here is code to check customer is logged in.

/**
 * @var \Magento\Customer\Model\Session
 */
protected $customerSession;

/**
 * @param \Magento\Customer\Model\Session $customerSession
 */
public function __construct(
    \Magento\Customer\Model\Session $customerSession
){
    $this->customerSession = $customerSession;
}

public function execute()
{
    if ($this->customerSession->isLoggedIn()) {
          //some logic
    }
}

to check customer just logged in you could observe "customer_customer_authenticated" event

Vendor/Module/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_customer_authenticated">
        <observer name="vendor_customer_authenticated" instance="Vendor\Module\Observer\CustomerAuthenticated" />
    </event>
</config>

Vendor\Module\Observer\CustomerAuthenticated.php

<?php
namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer as EventObserver;

class CustomerAuthenticated implements ObserverInterface
{
    public function execute(EventObserver $observer)
    {
        //some logic
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top