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.
I added this code in helper file, but when this code executed, it is not showing username on my account section in header, and when i proceed to place order, it is asking me to login again using popup in my site.