7

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?

0

3 Answers 3

21

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;
    }
}
3
  • can i get customer session in this observer?
    – Hunter
    Jun 14, 2017 at 9:30
  • Yes you can get customer data from $observer. check my updated customerLogin.php Jun 14, 2017 at 11:00
  • hi @prince patel is this event/concept used for to logged customer while using in REST Apis? Nov 4, 2017 at 5:03
1

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
    }
}
3
  • i need to check if customer has just logged in, not checking whether customer is logged in or not
    – Hunter
    Jun 14, 2017 at 3:07
  • to check customer just logged in you could observe "customer_customer_authenticated" event Jun 14, 2017 at 3:09
  • in that observer i try to check if($this->customerSession->isLoggedIn()) {die('in');} but it didnt effect at all
    – Hunter
    Jun 14, 2017 at 9:30
1

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!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.