My requirement is when user comes from and external php website via url they will pass the email id in web url request. I need to catch that email and start the magento2 customer session. I searched in google and placed my code as follows.
$data=$this->getRequest()->getParams();
if(isset($data['email'])){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// Load customer By Email
$customer = $objectManager->create('Magento\Customer\Model\Customer')->setWebsiteId(1)->loadByEmail($data['email']);
// Create customer session
echo $customer->getId();
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setCustomerAsLoggedIn($customer);
if($customerSession->isLoggedIn()) {
echo "Customer Logged in";
}else{
echo "customer is Not Logged in";
}
// $customerSession->setCustomerDataAsLoggedIn($customer);
$customerSession->regenerateId();
}
I placed this in header.phtml file. Why because this file must be included everywhere. So when email presented in URL i am doing autologin. But session is not creating for user. When i click on any link called as "My Account" it is asking me to login again.