Solved! Go to Solution.
Hello @bcadevteam
You can manage the customer login using a custom attribute.
Suppose there is a custom attribute code "allow_login" with the options of "Yes (1)" and "No (0)"
Now you can create a plugin to check if the customer is allowed to login or not.
Declare the plugin in your custom module.
app/code/Vendor/Module/etc/frontend/di.xml
<?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="check_customer_login_permission" type="Vendor\Module\Plugin\CheckCustomerLoginPermission" />
</type>
</config>Create your plugin class
app/code/Vendor/Module/Plugin/CheckCustomerLoginPermission.php
<?php
namespace Vendor\Module\Plugin;
class CheckCustomerLoginPermission
{
protected $_customerRepository;
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
) {
$this->_customerRepository = $customerRepository;
}
public function beforeExecute(\Magento\Customer\Controller\Account\LoginPost $subject)
{
$loginData = $subject->getRequest()->getPost('login');
$email = $loginData['username'];
$customer = $this->_customerRepository->get($email);
if ($customer->getId()) {
$customerData = $customer->__toArray();
$isCustomerAllowedToLogin = $customerData['custom_attributes']['allow_login']['value'];
if ($isCustomerAllowedToLogin == 0) {
// do whatever you want when the customer is not allowed to login
// for example:
// return the customer to login page and show an error message that you are now allowed to login.
}
}
}
}
Hello @bcadevteam
You can manage the customer login using a custom attribute.
Suppose there is a custom attribute code "allow_login" with the options of "Yes (1)" and "No (0)"
Now you can create a plugin to check if the customer is allowed to login or not.
Declare the plugin in your custom module.
app/code/Vendor/Module/etc/frontend/di.xml
<?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="check_customer_login_permission" type="Vendor\Module\Plugin\CheckCustomerLoginPermission" />
</type>
</config>Create your plugin class
app/code/Vendor/Module/Plugin/CheckCustomerLoginPermission.php
<?php
namespace Vendor\Module\Plugin;
class CheckCustomerLoginPermission
{
protected $_customerRepository;
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
) {
$this->_customerRepository = $customerRepository;
}
public function beforeExecute(\Magento\Customer\Controller\Account\LoginPost $subject)
{
$loginData = $subject->getRequest()->getPost('login');
$email = $loginData['username'];
$customer = $this->_customerRepository->get($email);
if ($customer->getId()) {
$customerData = $customer->__toArray();
$isCustomerAllowedToLogin = $customerData['custom_attributes']['allow_login']['value'];
if ($isCustomerAllowedToLogin == 0) {
// do whatever you want when the customer is not allowed to login
// for example:
// return the customer to login page and show an error message that you are now allowed to login.
}
}
}
}