I want to allow the customer to Log in using email or mobile number on the checkout page and the login page.

Note: Mobile number is customer eav attribute.


1 Answer 1


For Login with the customer, First you need to make mobile number required field in customer Registration form. Whenever customer register, At that time you need to add a field like mobile no. and save that field to Customer registration page.

Now when customer login at that time they enter email id or mobile number. For this you need to override this controller


in your module and add code like below, Path should be like: app/code/Vendor/Modulename/Controller/Account/LoginPost.

Now keep below code in your controller,

namespace Vendor\Modulename\Controller\Account;

use Magento\Customer\Model\Url as CustomerUrl;
use Magento\Framework\Data\Form\FormKey\Validator;
use Magento\Customer\Model\Account\Redirect as AccountRedirect;

class LoginPost extends \Magento\Customer\Controller\Account\LoginPost
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Api\AccountManagementInterface $customerAccountManagement,
        CustomerUrl $customerHelperData,
        Validator $formKeyValidator,
        AccountRedirect $accountRedirect,
        \Magento\Customer\Model\ResourceModel\Customer\CollectionFactory $customerFactory
    ) {
        $this->_customerFactory = $customerFactory;

    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(!strpos($login['username'], '@') !== false ) {
                $isMobile = $login['username']; 
                /* Get email id based on mobile number and login*/
                $customereCollection = $this->_customerFactory->create();
                $customereCollection->addFieldToFilter("mobile", $login['username']);
                foreach($customereCollection as $customerdata){ 
                    $login['username'] = $customerdata['email'];

            if (!empty($login['username']) && !empty($login['password'])) {
                try {
                    $customer = $this->customerAccountManagement->authenticate($login['username'], $login['password']);
                    if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
                        $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
                        $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
                    $redirectUrl = $this->accountRedirect->getRedirectCookie();

                    if (!$this->getScopeConfig()->getValue('customer/startup/redirect_dashboard') && $redirectUrl) {

                        $resultRedirect = $this->resultRedirectFactory->create();
                        // URL is checked to be internal in $this->_redirect->success()
                        return $resultRedirect;
                        $resultRedirect = $this->resultRedirectFactory->create();
                        return $resultRedirect;
                } catch (EmailNotConfirmedException $e) {
                    $value = $this->customerUrl->getEmailConfirmationUrl($login['username']);
                    $message = __(
                        'This account is not confirmed. <a href="%1">Click here</a> to resend confirmation email.',
                } catch (UserLockedException $e) {
                    $message = __(
                        'You did not sign in correctly or your account is temporarily disabled.'
                } catch (AuthenticationException $e) {
                    $message = __('You did not sign in correctly or your account is temporarily disabled.');
                } catch (LocalizedException $e) {
                    $message = $e->getMessage();
                } catch (\Exception $e) {
                    // PA DSS violation: throwing or logging an exception here can disclose customer password
                        __('An unspecified error occurred. Please contact us for assistance.')
            } else {
                $this->messageManager->addError(__('A login and a password are required.'));

        return $this->accountRedirect->getRedirect();

I hope this will help

  • on checkout page, how can i change input type email to text ? so user can add their mobile number. Commented Apr 12, 2019 at 5:57
  • 1
    @CharviParikh override template Magento_Checkout/template/form/element/email.html Commented Apr 12, 2019 at 6:25

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.