3

I have programmatically logged into the customer accounts from my custom controller. In this case the customer name is not loading in the header. https://prnt.sc/nzucg9

How can I do it?

Here is the code

    public function __construct(
               .....
            \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerInterfaceFactory,
            \Magento\Store\Model\StoreManagerInterface $storeManagerInterface,
            \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
            \Magento\Customer\Model\CustomerFactory $customerFactory,
            \Magento\Customer\Model\Session $customerSession,
             ....
        ) {
            $this->customerInterfaceFactory = $customerInterfaceFactory;
            $this->customerRepositoryInterface = $customerRepositoryInterface;
            $this->storeManagerInterface = $storeManagerInterface;
            $this->customerFactory = $customerFactory;
            $this->customerSession = $customerSession;
                 .....

        }

 public function execute() {
    $email = '[email protected]';
        $storeId = $this->storeManagerInterface->getStore()->getId();             
        $websiteId = $this->storeManagerInterface->getStore($storeId)->getWebsiteId();      
        $customer = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId)->loadByEmail($email);
        $this->customerSession->setCustomerAsLoggedIn($customer);
        $this->customerSession->regenerateId();
}

1 Answer 1

3

That value comes from localstorage (stored in the browser), you should check that your code actually places/reloads the customer data in the localstorage, that can hardly be done from the backend, try to use js to interact with the local storage, take this answer: How to force reloading of a customer data section?

There is no easy way to invalidate data from backend. On frontend you can use app/code/Magento/Customer/view/frontend/web/js/customer-data.js library to invalidate the local storage. Just call customerData.reload(['section_name']);

In crhome console it will look like: require('Magento_Customer/js/customer-data').reload('customer');

Also you can use this module as an example on how they implement the localstorage reload: https://github.com/magefan/module-login-as-customer

6
  • I have used an ajax function to login to the account. I want to set the localstorage in the success function. How can I do it? Commented Jun 11, 2019 at 4:29
  • Check the answer now, haven't tried it but other people did it like that
    – SantiBM
    Commented Jun 11, 2019 at 6:25
  • I have tried it. But it is not working Commented Jun 11, 2019 at 6:37
  • 1
    It seems that you should interact with the local storage trough the frontend, check: magento.stackexchange.com/questions/208223/… Also check this free module, they use a js to reload the customer data: github.com/magefan/module-login-as-customer I'm using that module and works perfectly
    – SantiBM
    Commented Jun 11, 2019 at 8:25
  • Thank you so much SantiBM. The extension js works perfectly. Please update the answer. Commented Jun 11, 2019 at 9:05

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.