0

I want to redirect my store customers to custom login page when a customer visits my magento 2 store url. The customer could not access the site till he login to the store.

How could I do that in Magento 2.3.x?

1

Yes, you can achieve this. Create an observer on events controller_action_predispatch and then using customer session \Magento\Customer\Model\Session check every URL and redirect to Customer login page.

  • Create first events.xml where we can defined observer for your work.File location: app/code/{Vendor}/{ModuleName}/etc/frontend/events.xml
  • Create an observer class for checking customer login and redirect to logo page.app/code/{Vendor}/{ModuleName}/Observer/ControllerActionPredispatch.php

events.xml

<?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="controller_action_predispatch">
        <observer name="check_customer_session_login"
                  instance="{Vendor}\{ModuleName}\Observer\ControllerActionPredispatch"  />
    </event>
</config>

ControllerActionPredispatch.php

<?php
namespace {Vendor}\{ModuleName}\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\Action\Action;

class ControllerActionPredispatch implements ObserverInterface
{

    /**
     * @var \Magento\Customer\Model\Url
     */
    private $customerUrl;
    /**
     * @var \Magento\Customer\Model\Session
     */
    private $customerSession;
    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        \Magento\Customer\Model\Url $customerUrl,
        \Magento\Customer\Model\Session $customerSession,
        LoggerInterface $logger
    ){
        $this->customerUrl = $customerUrl;
        $this->customerSession = $customerSession;
        $this->logger = $logger;
    }

    /**
     * @inheritDoc
     */
    public function execute(Observer $observer)
    {
        /** @var Action $controller */
        $action = $observer->getEvent()->getControllerAction();
        $request = $observer->getEvent()->getRequest();
        $request->getRouteName();
        $this->logger->info($request->getRouteName());
        $loginUrl = $this->customerUrl->getLoginUrl();

        if (($request->getRouteName()!== 'customer') &&
            (!$this->customerSession->authenticate($loginUrl))
        ) {
            $action->getActionFlag()->set('', $action::FLAG_NO_DISPATCH, true);
        }

    }
}
4
  • I want to redirect to custom login page 'customer/account/custom-login/'. Is it possible using above method? – Rocking Raza Jun 2 '20 at 7:48
  • yes, it can do ` $this->customerUrl->getLoginUrl()`. Gives the Login url – Amit Bera Jun 2 '20 at 7:49
  • i used the above method it gives me HTTP ERROR 500. – Rocking Raza Jun 2 '20 at 8:23
  • please check the log files for 500 error, The code is test on 2.3. and it working fine – Amit Bera Jun 2 '20 at 8:53

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.