Categories: How ToMagento 2

How to Login Customer Programmatically without Password in Magento 2

Sometimes you require to create customer programatically and login it without password in Magento 2. In many cases where the login is not important and it is just required to check the functionality or to follow a process, you require quick login. One of the best use when you want to show users with Magento extensions demo where login is needed.

Here, the user experience will get worse if you force them to remember password or enter it. This may lead users to leave website and thus increases bounce rate of your website. Here, I have come up with a code trick to skip password and login customer programmatically.

First you need to use dependency injection to inject the following classes in your constructor.

protected $_customer;
protected $_customerSession;

public function _construct(...
                         \Magento\Customer\Model\Customer $customer,
                         \Magento\Customer\Model\Session $customerSession) {
    ...
    $this->_customer = $customer;
    $this->_customerSession = $customerSession;
    ...
}

Then in your code you can do:

$customer = $this->_customer->loadByEmail("johndoe@example.com"); 
$this->_customerSession->setCustomerAsLoggedIn($customer);

And you are done. Hope this guide has enough helped you to skip password and auto login customer programmatically. Let me know if you have any issue or want to give any suggestions through commenting.

Click to rate this post!
[Total: 12 Average: 3.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • Hello, i'm looking to connect a customer by mail and without password. How can i set the session ? This code just connect for current page or everypage ? Where i place it ? controller or block ?

    Regards !

    • Hello,
      This code sets the customer as logged in customer by using his email. You need to place this code as per your requirement. You have to set this code in the constructor of say helper, model, controller or block.

      Thanks

  • Hello, i'm looking to connect a customer by mail and without password. How can i set the session ? This code just connect for current page or everypage ? Where i place it ? controller or block ?

    Regards !

    • Hello,
      This code sets the customer as logged in customer by using his email. You need to place this code as per your requirement. You have to set this code in the constructor of say helper, model, controller or block.

      Thanks

Recent Posts

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

9 hours ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

2 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

4 days ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

5 days ago

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

5 days ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

5 days ago