1

I have created customer dashboard custom tab and the path is 'inquiry/customer/' which is working fine.

Issue is that If the user hit the direct that url in the browser without login in his account then user is getting an error.

I want to redirect the user to login page if he is not logged in.

How can I do?

My Code:

customer_account.xml:

<?xml version="1.0"?> 

<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-inquiry-link">
                <arguments>
                    <argument name="label" xsi:type="string" translate="true">My Question</argument>
                    <argument name="path" xsi:type="string">inquiry/customer</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Controller:

<?php
namespace  Vendor\Inquiry\Controller\Customer;

class Index extends \Magento\Framework\App\Action\Action
{
   /**
  * Index Action*
  * @return void
  */
  public function execute()
  {
    $this->_view->loadLayout();
    $this->_view->renderLayout();
  }
}

4 Answers 4

1

Try this code

protected $resultRedirect;
protected $customerSession;

public function __construct(
\Magento\Framework\Controller\ResultFactory $result,
\Magento\Customer\Model\Session $customerSession 
){
   $this->resultRedirect = $result;
   $this->customerSession = $customerSession;
}

public function execute()
{
    $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
   if($this->customerSession->isLoggedIn()){ 
      $resultRedirect->setUrl('customer/account/login');
      return $resultRedirect;         
   }
} 
3
  • After using your code in controller getting an error: {"0":"Type Error occurred when creating object: Mage2\\Inquiry\\Controller\\Customer\\Index\\Interceptor","1":"<pre>#1 Jan 8, 2020 at 5:52
  • here is solution magento.stackexchange.com/a/282611/78648
    – Waqar Ali
    Jan 8, 2020 at 5:57
  • or simply run php bin/magento s:up
    – Waqar Ali
    Jan 8, 2020 at 6:01
1

In your Controller file, you can add below code,

<?php

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Controller\Result\RedirectFactory $resultRedirectFactory,
    \Magento\Customer\Model\SessionFactory $customerSession
) {
    $this->resultRedirectFactory = $resultRedirectFactory;
    $this->customerSession=$customerSession;
    parent::__construct($context);
}
public function execute()
{
    if (!$this->customerSession->create()->isLoggedIn()) {
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('customer/account/login');
        return $resultRedirect;
    }
    // your code
}

Hope it helps.

6
  • custom tab created by xml and I am calling phtml file for getting the results. There is no controller used here. Jan 8, 2020 at 5:28
  • can you show your xml and phtml file?
    – Mohit Rane
    Jan 8, 2020 at 5:35
  • After using your code in controller getting an error: {"0":"Type Error occurred when creating object: Mage2\\Inquiry\\Controller\\Customer\\Index\\Interceptor","1":"<pre>#1 Jan 8, 2020 at 5:47
  • update my xml and controller Jan 8, 2020 at 5:52
  • please upgrade and deploy
    – Mohit Rane
    Jan 8, 2020 at 6:11
0

Create a helper class with the below __construct and our required method ( here named it as redirectIfNotLoggedIn) which redirects to login.

public function __construct(
    Magento\Framework\App\Helper\Context $context,
    Magento\Customer\Model\Session $customerSession,
)
{
    parent::__construct($context);
    $this->customerSession = $customerSession;
    $this->urlInterface = $context->getUrlBuilder();
}

public function redirectIfNotLoggedIn()
{
    if (!$this->customerSession->isLoggedIn()) {
        $this->customerSession->setAfterAuthUrl($this->urlInterface->getCurrentUrl());
        $this->customerSession->authenticate();
    }
}
1
  • I have data.php helper. But I have custom query in phtml there is no connection from helper, controller etc. Custom tab created by xml Jan 8, 2020 at 5:30
0

You can follow method to get it works.

Check if a customer is logged in or not:

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $customerSession = $objectManager->get('Magento\Customer\Model\Session');
    if($customerSession->isLoggedIn()) {
       // then put here your actual link e.g www.example.com/inquiry/customer/
    }else{
// put login link here e.g. www.example.com/customer/account/login
}

Note: do not use object manager you get more code to check logged in here: How to check if customer is logged in or not in magento 2?

Edit: Check Mohit Rane answer further you can add a check in your execute function :

 public function execute()
  {
if (!$this->customerSession->isLoggedIn()) {
        $resultRedirect = $this->resultRedirectFactory->create();
        $resultRedirect->setPath('customer/account/login');
        return $resultRedirect;
    }else {
    $this->_view->loadLayout();
    $this->_view->renderLayout();
}
  }
2
  • Using this content is not showing but page still opening which must be redirect to login page Jan 8, 2020 at 5:25
  • @VinodKumar check updated answer
    – Arunendra
    Jan 8, 2020 at 5:56

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.