Skip to content

Instantly share code, notes, and snippets.

@jasonevans1
Created May 19, 2022 22:33
Show Gist options
  • Save jasonevans1/d8f54a647d597c55ade10d9f5483ac3a to your computer and use it in GitHub Desktop.
Save jasonevans1/d8f54a647d597c55ade10d9f5483ac3a to your computer and use it in GitHub Desktop.
Magento Customer Authenticated Plugin
<?php
declare(strict_types=1);
namespace Abc\Customer\Plugin;
use Magento\Customer\Api\AccountManagementInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\Exception\NoSuchEntityException;
class CustomerAuthenticated
{
private CustomerRepositoryInterface $customerRepository;
public function __construct(CustomerRepositoryInterface $customerRepository)
{
$this->customerRepository = $customerRepository;
}
public function beforeAuthenticate(AccountManagementInterface $subject, $username, $password): array
{
try {
$customer = $this->customerRepository->get($username);
} catch (NoSuchEntityException $e) {
throw new \Magento\Framework\Exception\InvalidEmailOrPasswordException(__('Invalid login or password.'));
}
if ($customer->getExtensionAttributes()
&& $customer->getExtensionAttributes()->getCompanyAttributes()
&& $customer->getExtensionAttributes()->getCompanyAttributes()->getActivated() == 0
) {
throw new \Magento\Framework\Exception\LocalizedException(__('This account is locked.'));
}
return [$username, $password];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment