0

I would like to redirect the customer from any page of the store to the login page if he isn't already logged in. I guess I need to make a plugin or an observer on some dispatch or forward method, but I can't guess which one is the one to check any new called url. Does any of you already did that ? Thanks.

1 Answer 1

1

You can do with the use of events. Please add below events and observer code in your custom module.

step 1: Please create file events.xml under path PackageName/Module/etc/frontend/

Write below code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="redirectcustomer_success" instance="PackageName\Module\Observer\RedirectCustomer" />
    </event>
</config>

Step 2: Please create file RedirectCustomer.php under path PackageName/Module/Observer/

<?php
namespace PackageName\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Response\Http as HttpResponse;

class RedirectCustomer implements ObserverInterface
{
     /**
     * @var \Magento\Framework\App\ResponseFactory
     */
    protected $responseFactory;

    protected $_customerSession;

    protected $_customerUrl;

    protected $_urlInterface;

    protected $request;

    public function __construct(
        \Magento\Framework\App\ResponseFactory $responseFactory,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Url $customerUrl,
        \Magento\Framework\UrlInterface $urlInterface,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->responseFactory = $responseFactory;
        $this->_customerSession = $customerSession;
        $this->_customerUrl = $customerUrl;
        $this->_urlInterface = $urlInterface;
        $this->request = $request;
    }

    public function execute(Observer $observer)
    {   
        $redirectUrl = $this->getCustomerLoginUrl(); 
        $currentUrl = $this->_urlInterface->getCurrentUrl(); 
        $fullActionName = $this->request->getFullActionName();

        if(!$this->_customerSession->isLoggedIn() && $currentUrl != $redirectUrl &&  $fullActionName != 'customer_account_forgotpassword' && $fullActionName != 'customer_account_createpassword' && $fullActionName != 'customer_section_load') {
            $resultRedirect = $this->responseFactory->create();
            $resultRedirect->setRedirect($redirectUrl)->sendResponse('200');
            exit();
        }   
    }

    public function getCustomerLoginUrl() 
    {   
        return $this->_customerUrl->getLoginUrl();
    }
}

In above code, we are allowing forgot password and rest password. If you don't want then you can remove it from condition.

Please check and let us know

Cheers!

1
  • 1
    Was trying to do that with a Plugin of getUrl and I was struggling. Your way is the good one thanks ;)
    – Claims
    Commented Jun 14, 2021 at 6:51

Your Answer

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

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