SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH
autologin

Login Programmatically only using email without password for Magento 2

In some cases when you are asked to log in automatically without using a password in Magento 2 So today in this article I will show you one of the best ways to show users a demo of the Magento extension where they need to log in.

First, you need to use dependency injection in the constructor for the class you want to implement auto-login. Here, I use two classes \Magento\Customer\Model\Customer $customer, \Magento\Customer\Model\Session $customerSession.

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 class, you can call loadByEmail() function and setCustomerAsLoggedIn() function for your autologin purpose.

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

Note that in the loadByEmail() function, you will pass the parameter inside which expresses the email you want to log in automatically.

The above is a tip to help you perform login automatically. Remember that the user experience can be extremely bad if they are forced to constantly log in with both user name and password every time they log out. This will cause your website to have negative feedback because of the inconvenience. That’s why enhancing the user experience by using auto login is a must.