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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago