I have programmatically logged into the customer accounts from my custom controller. In this case the customer name is not loading in the header. https://prnt.sc/nzucg9
How can I do it?
Here is the code
public function __construct(
.....
\Magento\Customer\Api\Data\CustomerInterfaceFactory $customerInterfaceFactory,
\Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\Session $customerSession,
....
) {
$this->customerInterfaceFactory = $customerInterfaceFactory;
$this->customerRepositoryInterface = $customerRepositoryInterface;
$this->storeManagerInterface = $storeManagerInterface;
$this->customerFactory = $customerFactory;
$this->customerSession = $customerSession;
.....
}
public function execute() {
$email = '[email protected]';
$storeId = $this->storeManagerInterface->getStore()->getId();
$websiteId = $this->storeManagerInterface->getStore($storeId)->getWebsiteId();
$customer = $this->customerFactory->create();
$customer->setWebsiteId($websiteId)->loadByEmail($email);
$this->customerSession->setCustomerAsLoggedIn($customer);
$this->customerSession->regenerateId();
}