Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5488

How to login customer with just email in Magento 2

$
0
0

In this blog, I will explain how you can login any customer without password.

Here we will load the customer model and set it in the session. In the below example we have loaded the customer with email “test@webkul.com“.

public function __construct(
    .....
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepo,
    \Magento\Customer\Model\CustomerFactory $customerFactory,
    \Magento\Customer\Model\Session $customerSession,
    .....
) {
    .....
    $this->customerRepo = $customerRepo;
    $this->customerFactory = $customerFactory;
    $this->customerSession = $customerSession;
    .....
}

public function execute()
{
    .....
    $customerRepo = $this->customerRepo->get("test@webkul.com");                    //load with email
    $customer = $this->customerFactory->create()->load($customerRepo->getId());     //get the customer model by id
    $this->customerSession->setCustomerAsLoggedIn($customer);                       //set the customer to the session
    .....
}

Feel free to comment if you have any issue.

 

Change Customer Password Programmatically in Magento2


Viewing all articles
Browse latest Browse all 5488

Trending Articles