I am creating a new extesnion in Magento 2. I need to send email regarding customer login failed to admin. Is there any event for customer login failed?
2 Answers
Please try the below code in your custom extension to check if the customer login failed!
In the di.xml file of custom extension:
<?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\Model\AccountManagement" type="vendor\module\Model\AccountManagement" />
</config>
And add the AccountManagement.php file under the Model directory
<?php
namespace vendor\module\Model;
class AccountManagement extends \Magento\Customer\Model\AccountManagement
{
public function authenticate($username, $password)
{
//Add the code below as per your requirement
try {
$customer = $this->customerRepository->get($username);
} catch (NoSuchEntityException $e) {
throw new InvalidEmailOrPasswordException(__('Invalid login or password.'));
}
$customerId = $customer->getId();
if ($this->getAuthentication()->isLocked($customerId)) {
throw new UserLockedException(__('The account is locked.'));
}
try {
$this->getAuthentication()->authenticate($customerId, $password);
} catch (InvalidEmailOrPasswordException $e) {
throw new InvalidEmailOrPasswordException(__('Invalid login or password.'));
}
if ($customer->getConfirmation() && $this->isConfirmationRequired($customer)) {
throw new EmailNotConfirmedException(__("This account isn't confirmed. Verify and try again."));
}
$customerModel = $this->customerFactory->create()->updateData($customer);
$this->eventManager->dispatch(
'customer_customer_authenticated',
['model' => $customerModel, 'password' => $password]
);
$this->eventManager->dispatch('customer_data_object_login', ['customer' => $customer]);
return $customer;
}
}
Hope this helps!
-
-
As you can see the function authenticate the customer account, username and password are valid or not and more! You can add your custom code in function when there's an exception in the case of 'Invalid login or password'. Commented May 18, 2020 at 11:07
Magento 2 does not have any event by which you can check customer login failed. Magento login validation check from \Magento\Customer\Model\AccountManagement::authenticate
for customization .
So, you want any customization then you have to work base on the method login failed. Magento login validation check from
-
So how can i trigger a function in my custom extension when login failed? Commented May 14, 2020 at 11:32