0

I have created a new tab in my account page. customer_account.xml page as below,

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="customer-account-navigation-custom">
                <arguments>
                    <argument name="path" xsi:type="string">ucssms/verification/index</argument>
                    <argument name="label" xsi:type="string">Verify Mobile</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

However, the link is not checking if there is an active customer session. So, I added a manual check as below,

<?php

namespace Ucs\Reson8\Controller\Verification;

class Index extends \Magento\Framework\App\Action\Action
{
    /** @var  \Magento\Framework\View\Result\Page */
    protected $_customerSession;
    protected $_resultRedirectFactory;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,

        \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
        \Magento\Customer\Model\Session $customerSession
    )
    {

        $this->_resultRedirectFactory = $resultRedirectFactory;
        $this->_customerSession = $customerSession;
        parent::__construct($context);
    }
    /**
     * Blog Index, shows a list of recent blog posts.
     *
     * @return \Magento\Framework\View\Result\PageFactory
     */
    public function execute()
    {
        if (!$this->_customerSession->isLoggedIn()) {
            $resultRedirect = $this->_resultRedirectFactory->create();
            $resultRedirect->setPath('customer/account/login');
            return $resultRedirect;
            
        }

        $this->_view->loadLayout(); 
        $this->_view->renderLayout(); 
    }
}

It works fine. But if logged out due to session timeout, after login, it is not automatically redirecting to ucssms/verification/index handler?

Is there a native method in magento 2 to handle?

Your Answer

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

Browse other questions tagged or ask your own question.