cancel
Showing results for 
Search instead for 
Did you mean: 

I need to login customer by mobile number in magento 2.2.2

I need to login customer by mobile number in magento 2.2.2

please anyone help me to login customer by mobile number.  I'm new to magento2 

13 REPLIES 13

Re: I need to login customer by mobile number in magento 2.2.2

Hi @ask_xah

 

i understand , but by default there is no functionality to login with mobile number available in magento 2.

 

so you need to create custom module for this kind of functionality.

 

There are also third - party extension available it does the same functionality you are going to achieve .

 

below are the extensions link for the reference :

 

https://codecanyon.net/item/mobile-number-login-Magento-2-extension/19720124

 

https://cedcommerce.com/magento-2-extensions/customer-mobile-login

 

https://magecomp.com/magento-2-mobile-login.html

 

Hope it helps 

if issue solved,Click Kudos & Accept as Solution

Re: I need to login customer by mobile number in magento 2.2.2

i known there are extension every where.. But, I have no fund for purchase them.. so that i want to do it own.

Re: I need to login customer by mobile number in magento 2.2.2

Hi @ask_xah

 

I understand your concern !! but as of now there is no native functionality in magento 2.

 

Might be in future magento will adapt this kind of functionality in their code base.

 

so as of now you either need to go with the third party extension  or you need to create custom module from sketch.

 

Hope it helps !

 

 

if issue solved,Click Kudos & Accept as Solution

Re: I need to login customer by mobile number in magento 2.2.2

Do you have this extension.? If possible can you please share this to me, I will be more helpful to me.. I'm a open cart developer  completely  new to magento Its frame work are too hard to understand.  thanks in advance Smiley Happy

Re: I need to login customer by mobile number in magento 2.2.2

For Login with the customer, First you need to required field for Mobile at Registration time.

Whenever customer registration 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 you need to override,

Magento\Customer\Controller\Account\LoginPost

controller in your module and add code like below,

Now keep below code in your controller,

<?php
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;
        parent::__construct($context,$customerSession,$customerAccountManagement,$customerHelperData,$formKeyValidator,$accountRedirect);
    }

    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(!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']);
                    $this->session->setCustomerDataAsLoggedIn($customer);
                    $this->session->regenerateId();
                    if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
                        $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
                        $metadata->setPath('/');
                        $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
                    }
                    $redirectUrl = $this->accountRedirect->getRedirectCookie();

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

                        $this->accountRedirect->clearRedirectCookie();
                        $resultRedirect = $this->resultRedirectFactory->create();
                        // URL is checked to be internal in $this->_redirect->success()
                        $resultRedirect->setUrl($this->_redirect->success($redirectUrl));
                        return $resultRedirect;
                    }else{
                        $resultRedirect = $this->resultRedirectFactory->create();
                        $resultRedirect->setPath('');
                        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.',
                        $value
                    );
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (UserLockedException $e) {
                    $message = __(
                        'You did not sign in correctly or your account is temporarily disabled.'
                    );
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (AuthenticationException $e) {
                    $message = __('You did not sign in correctly or your account is temporarily disabled.');
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (LocalizedException $e) {
                    $message = $e->getMessage();
                    $this->messageManager->addError($message);
                    $this->session->setUsername($login['username']);
                } catch (\Exception $e) {
                    // PA DSS violation: throwing or logging an exception here can disclose customer password
                    $this->messageManager->addError(
                        __('An unspecified error occurred. Please contact us for assistance.')
                    );
                }
            } else {
                $this->messageManager->addError(__('A login and a password are required.'));
            }
        }

        return $this->accountRedirect->getRedirect();
    }
}
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: I need to login customer by mobile number in magento 2.2.2

Hi @ask_xah

 

I understand your situation as a open cart developer its difficult to directly work on magento 2.

 

But i don't have this kind of extension available with me as of now so unable to share with you.

 

Hope you understand !!!

 

 

if issue solved,Click Kudos & Accept as Solution

Re: I need to login customer by mobile number in magento 2.2.2

how can i implement your code.. just replace all with your code or just adding your ?

Re: I need to login customer by mobile number in magento 2.2.2

You need to create module and override above controller in your module with above code.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: I need to login customer by mobile number in magento 2.2.2

please any tutorials for creating and overriding module controller..?