Magento 2 Login Customer Programmatically


Magento 2 Login Customer Programmatically– There are many ways to login customer programmatically in Magento2. It is very easy to set customer as logged in Magento2. Here in this tutorial we are going to explain how you can log in any customer as logged in.


Magento 2 Login Customer Programmatically By Email Id | Magento2 Example

Here in this article we are going to login on Frontend as an user without password using customer email Id. You can load customer by Email Id and login as below –

Magento 2 Login Customer Programmatically Without Password Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// Load customer By Email
$customer = $objectManager->create('Magento\Customer\Model\Customer')->loadByEmail('xyz@example.com');

// Create customer session
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
$customerSession->setCustomerAsLoggedIn($customer);

if($customerSession->isLoggedIn()) {
    echo "Customer Logged in Successfully";
}else{
    echo "Unable to Login";
}

So you can use this method to login anywhere any customer.


Advertisements

Add Comment

📖 Read More