I've added a customer custom data (code_client) to the customer info and I want the user to be able to login with both the email addresses and that code_client.

Is it possible to log in to my store without using only an email address?

Find some hint on the stack : Magento 2: Get Customer data before login with observer

But there's no mention of the password and how to check it.

  • I'm currently trying AdvancedLogin, thanks, I'll let you know I this is working. Commented Jan 21, 2020 at 11:06
  • @MorganTartreau I can help you, let me know a few things first, did you create custom customer attribute? Commented Jan 21, 2020 at 11:17
  • Yes I did, all my user have this attribute. I've configured the plugin in admin panel, I'm trying to display the right login form (i'm showing the default login form as I'm using a custom theme) Commented Jan 21, 2020 at 11:20
  • You need to rewrite the login action or the authetnticate method and change logic there. Commented Jan 21, 2020 at 11:26

1 Answer 1


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



change type=emailtotype=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" />

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" />

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();
        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'])
                    if($login['username'] == $getCustomer->getUsername()){
                        $login['username'] = $getCustomer->getEmail();
                //rest of the code

Let me know if need any help!!

  • Not tested, but I think I would recommand this method if you want to make a custom plugin. The AdvancePlugin (github.com/semaio/Magento2-AdvancedLogin) works perfectly. Thanks anyway ! Commented Jan 21, 2020 at 12:33

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.