Back to Top

How to login customer with just email in Magento 2

Updated 1 March 2024

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 “[email protected]“.

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("[email protected]");                    //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.

https://webkul.com/blog/change-customer-password-programmatically-in-magento2/

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


1 comments

  • Nilesh
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home