I have a flow where I need to verify if user has verified their phone number, if not I have redirected them to verification page in my module, once the user verify their OTP sent to their number I need to auto login user, likewise it is done in the case of email verification, once user click to verification code they are auto logged in into their account

  • what issue you are facing, where you are stuck ? – Naveed Asim Mar 27 '18 at 16:30
  • Dont find anywhere how to di that, that is why I didnt share any code, since I dont have any code yet – riksof-zeeshan Mar 28 '18 at 6:26

You can follow this blog to see how you can login using customer id, it is very useful


$customer_id = 1;
$objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->get('Magento\Customer\Model\Customer')->load($customer_id);
$customerSession = $objectManager->create('Magento\Customer\Model\Session');
  • Using Object Manager directly doesn't follow Magento best practice. – Khoa TruongDinh Mar 28 '18 at 4:39
  • @KhoaTruongDinh but $customerSession->setCustomerAsLoggedIn($customer); is it a good way to go? I mean will it do what is required and magento will allow when I publish my plugin with magento? – riksof-zeeshan Mar 28 '18 at 6:27
  • 1
    @riksof-zeeshan Not check yet. But, I think setCustomerAsLoggedIn($customer); is the key. – Khoa TruongDinh Mar 28 '18 at 6:34
  • Yes @KhoaTruongDinh, I agree with you , but this is an example of how to login by customer id, the object Manager should be initialize in the constructor, as well the Customer. Thank you for clarify this point – WISAM HAKIM Mar 28 '18 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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