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();
  }
}
Vinod Kumar
  • 2,045
  • 1
  • 23
  • 67

4 Answers4

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.

Mohit Rane
  • 1,955
  • 1
  • 13
  • 47
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;         
   }
} 
Waqar Ali
  • 2,319
  • 16
  • 44
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();
    }
}
nikin
  • 1,132
  • 9
  • 18
  • 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 – Vinod Kumar Jan 08 '20 at 05: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();
}
  }
Arunendra
  • 7,446
  • 3
  • 29
  • 54