0

I recently upgraded my Magento version from 2.4.2 to 2.4.3, The following code is not working properly (Session and Cookies not working) while it's working fine in the previous version:

protected $_customer;
protected $_customerSession;

    public function _construct(...
                             \Magento\Customer\Model\Customer $customer,
                             \Magento\Customer\Model\Session $customerSession) {
        ...
        $this->_customer = $customer;
        $this->_customerSession = $customerSession;
        ...
    }
    $customer = $this->_customer->loadByEmail("[email protected]"); 
    $this->_customerSession->setCustomerAsLoggedIn($customer);

When I clicked on "Proceed to checkout" button the login pop again asking for login. While when I'm using login with password then it's working fine.

2 Answers 2

0

Not sure where you're executing the code, but to log in the customer and update the section data, you have to use a POST-request.

You saying that you click the "Proceed to checkout" button signals that you may be using a GET-request.

I found the demonstration in this module quite helpful when logging in a customer via email: https://github.com/flagbit/magento2-virtual-customer-session/

0

I have modified the code as follows, and it's working fine.

<?php

namespace Arunendra\OtpLogin\Controller\Ajax;

use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\Cookie\PhpCookieManager;
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;

class Userotp extends \Magento\Framework\App\Action\Action
{
    ......
    protected $cookieMetadataManager;
    protected $cookieMetadataFactory;

    public function __construct(
        Context $context,
      ......
    )
    {
        parent::__construct($context);
   ......
    }

    public function execute()
    {
        ......
        $websiteId =$this->storeManager->getStore()->getWebsiteId();
        $customer = $this->_customer->setWebsiteId( $websiteId)->loadByEmail($isEmail['email']); 
        $this->refresh($customer);
        ......
    }
 
    /* Login otp issue*/
    public function refresh($customer)
    {

        if ($customer && $customer->getId()) {
            $this->_customerSession->setCustomerAsLoggedIn($customer);
            $this->_customerSession->regenerateId();

            if ($this->getCookieManager()->getCookie('mage-cache-sessid')) {
                $metadata = $this->getCookieMetadataFactory()->createCookieMetadata();
                $metadata->setPath('/');
                $this->getCookieManager()->deleteCookie('mage-cache-sessid', $metadata);
            }
        }
    }
      private function getCookieManager()
    {
        if (!$this->cookieMetadataManager) {
            $this->cookieMetadataManager = ObjectManager::getInstance()->get(
                PhpCookieManager::class
            );
        }

        return $this->cookieMetadataManager;
    }
    /**
     * Retrieve cookie metadata factory
     *
     * @deprecated
     * @return \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    private function getCookieMetadataFactory()
    {
        if (!$this->cookieMetadataFactory) {
            $this->cookieMetadataFactory = ObjectManager::getInstance()->get(
                CookieMetadataFactory::class
            );
        }

        return $this->cookieMetadataFactory;
    }
}

Your Answer

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

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