3

Need code for login customer programmatically without password in magento 2 enterprise edition?

If I have customer email id and I want to login without password in magento 1.X by code Here the code:

$email = '[email protected]';
 $customer = Mage::getModel('customer/customer');
 $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
 $customer->loadByEmail(trim($email));
 Mage::getSingleton('customer/session')->loginById($customer->getId());

But i want to use this in Magento 2 EE.

Any solution, Thanks in advance!

1

3 Answers 3

11

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("[email protected]"); 
$this->_customerSession->setCustomerAsLoggedIn($customer);
11
  • Could you please paste some sample code here Aug 23, 2016 at 9:08
  • @AmalEThomas what do you mean? My answer is a code sample Aug 23, 2016 at 9:18
  • I have android app, website for shopping, when the customer go to cart in shopping cart app, it will load the shopping cart to app from web. Here the customer don't want to login again. So i will pass the customer email via url to login without password. I will have to use above script to load the cart page without login while load web cart page in app. 1) Where i have place this code? Aug 23, 2016 at 9:30
  • @Rapheal, I am new to magento 2, Where and How i place this code sample? Aug 23, 2016 at 9:35
  • @AmalEThomas well that's an entire module to develop here there's no short answer sorry Aug 23, 2016 at 10:00
0

You can follow that links for login customer/admin without password:

0

Yes, We can Magento 2 login customer using an external script. We need to create one external script and run it on the browser. We need to check this Link

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.