Menu Close
    Hire Us Request Quote Reading List Switch to Dark Mode

    How to login customer with just email in Magento 2

    In this blog, I will explain how you can login any customer without password.

    Here we will load the customer model and set it in the session. In the below example we have loaded the customer with email “test@webkul.com“.

    public function __construct(
        .....
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepo,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        \Magento\Customer\Model\Session $customerSession,
        .....
    ) {
        .....
        $this->customerRepo = $customerRepo;
        $this->customerFactory = $customerFactory;
        $this->customerSession = $customerSession;
        .....
    }
    
    public function execute()
    {
        .....
        $customerRepo = $this->customerRepo->get("test@webkul.com");                    //load with email
        $customer = $this->customerFactory->create()->load($customerRepo->getId());     //get the customer model by id
        $this->customerSession->setCustomerAsLoggedIn($customer);                       //set the customer to the session
        .....
    }

    Feel free to comment if you have any issue.

     

    Change Customer Password Programmatically in Magento2

    . . .

    Comment

    Add Your Comment

    Be the first to comment.

    Back to Top
    css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close