I am assuming that code_client
is your custom attribute and you already store your custom attribute during customer registration.
First, please check that your code_client
is storing value or not, you can find it on customer_entity_<type of your code_client>
table, for example, if code_client
is varchar you can find value in 'customer_entity_varchar' where code_client's
attribute_id is mentioned.
if its storing unique value for every customer you can now focus on login with that attribute.
Override login.phtml
Path:
Magento/module-customer/view/frontend/templates/form/login.phtml
change type=email
totype=email
& remove data-validate for email.
<input name="login[username]" value="<?= $block->escapeHtmlAttr($block->getUsername()) ?>" <?php if ($block->isAutocompleteDisabled()) : ?> autocomplete="off"<?php endif; ?> id="email" type="text" class="input-text" title="<?= $block->escapeHtmlAttr(__('Email')) ?>" data-mage-init='{"mage/trim-input":{}}' data-validate="{required:true}">
In your routes.xml
<route id="username" frontName="username">
<module name="Vendor_Module" before="Magento_Customer" />
</route>
Create etc/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Controller\Account\LoginPost" type="Vendor\Module\Controller\Index\LoginPost" />
</config>
In your Controller: LoginPost.php
Please add in to your __construct (\Magento\Customer\Model\Customer $customer)
public function execute()
{
if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {
/** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
$resultRedirect = $this->resultRedirectFactory->create();
$resultRedirect->setPath('*/*/');
return $resultRedirect;
}
if ($this->getRequest()->isPost()) {
$login = $this->getRequest()->getPost('login');
if (!empty($login['username']) && !empty($login['password'])) {
try {
if (strpos($login['username'], '@') === false){
$getCustomer = $this->customer->getCollection()
->addAttributeToFilter('code_client', $login['username'])
->getFirstItem();
if($login['username'] == $getCustomer->getUsername()){
$login['username'] = $getCustomer->getEmail();
}
}
//rest of the code
Let me know if need any help!!