0

I'm having an issue where my "Default welcome msg!" does not get replaced with the customer name as which I'm logged in. I'm calling loginById() on the customer session in my own controller, and the dropdown next to it shows "My Account", "My Wish List" and "Sign Out", so that works alright as far as that goes.

The code I'm using is:

class Login extends \Magento\Framework\App\Action\Action
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $session;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
     */
    protected $cookieManager;

    /**
     * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
     */
    protected $cookieMetadataFactory;

    /**
     * Login constructor
     *
     * @param \Magento\Customer\Model\Session $session
     * @param \Magento\Framework\Stdlib\Cookie\PhpCookieManager $cookieManager
     * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
     * @param \Magento\Framework\App\Action\Context $context
     */
    public function __construct(
        \Magento\Customer\Model\Session $session,
        \Magento\Framework\Stdlib\Cookie\PhpCookieManager $cookieManager,
        \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
        \Magento\Framework\App\Action\Context $context
    ) {
        parent::__construct($context);

        $this->session = $session;
        $this->cookieManager = $cookieManager;
        $this->cookieMetadataFactory = $cookieMetadataFactory;
    }

    /**
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    public function execute()
    {
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());

        try {
            $customerId = 1;

            $this->session->regenerateId();
            $this->session->loginById($customerId);

            if ($this->cookieManager->getCookie('mage-cache-sessid')) {
                $metadata = $this->cookieMetadataFactory->createCookieMetadata();
                $metadata->setPath('/');
                $this->cookieManager->deleteCookie('mage-cache-sessid', $metadata);
            }

            $this->messageManager->addSuccessMessage(__('Successfully logged in '));
        } catch (\Exception $exception) {
            $this->messageManager->addErrorMessage(
                __('An error occurred while processing your login. Please try again later.')
            );
        }

        return $resultRedirect;
    }
}

When I try the same action for a second time (after first logging out) the header gets updated with my customer name and everything seems to be working as it's supposed to.

I checked how Magento is handling the login behavior and as far as I can see I'm basically doing the exact same thing (except for using loginById instead of authenticating the customer).

0

Solved it by turning my request into a POST and adding a sections.xml containg my route. See https://github.com/magento/magento2/issues/3287 for more information about this matter.

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.