1

I am using Magento version 2.3 Varnish 6 enabled Docker setup

I am implementing force login on event 'controller_action_predispatch'. Below is my code:

app/code/Vendor/Module/etc/frontend/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="kernel_request_before" instance="Vendor\Module\Observer\FrontSendResponseBefore" shared="false" />
    </event>
</config>

app/code/Vendor/Module/Observer/FrontSendResponseBefore.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class FrontSendResponseBefore implements ObserverInterface
{
    private $_helper;

    /**
    * @var \Magento\Framework\App\Response\RedirectInterface
    */
    protected $redirect;

    private $_objectManager;

    public function __construct(
        \Vendor\Module\Helper\CoreHelper $helper,
        \Magento\Framework\App\Response\RedirectInterface $redirect,
        \Magento\Framework\ObjectManagerInterface $objectmanager
    ) {
        $this->_helper = $helper;
        $this->redirect = $redirect;
        $this->_objectManager = $objectmanager;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $actionName = $observer->getEvent()->getRequest()->getFullActionName();
        $controller = $observer->getControllerAction();

        $allowedUrls = array(
            'customer_account_login', 
            'customer_account_loginPost',
            'customer_account_logout', 
            'customer_account_logoutSuccess',
            'customer_account_forgotpassword',
            'customer_account_forgotpasswordpost',
            'customer_account_createPassword', 
            'customer_account_createpassword',
            'customer_account_resetpasswordpost'
        );

        $customerSession = $this->_objectManager->create('Magento\Customer\Model\SessionFactory')->create();

        $redirect = true;
        if (!$this->ifForceLoginEnabledForStore()) {
            $redirect = false;
        } else if (in_array($actionName, $allowedUrls)) {
            $redirect = false;
        } elseif ($customerSession->isLoggedIn()) {
            $redirect = false;
        }

        echo 4; die;

        if ($redirect) {
            $this->redirect->redirect($controller->getResponse(), 'customer/account/login');
        }
    }

    private function ifForceLoginEnabledForStore()
    {
        $storeId = $this->_helper->getCurrentStoreId();

        if ($this->_helper->getConfigValue('general/general/forcelogin', $storeId) == true) {

            return true;
        }

        return false;
    }
}

As you see I have added a die statement, but it does not execute when the cached version of page is loaded.

Does anyone has any idea how to fix this?

0

That predispatch event is not dispatched when loading cached content.

You could plugin to the load method, either before or after.

In di.xml:

<type name="Magento\Framework\App\PageCache\Cache">
    <plugin name="some-name" type="Vendor\Module\Model\App\PageCachePlugin"/>
</type>

In your new class:

public function afterLoad(
    \Magento\Framework\App\PageCache\Cache $subject,
    $result
) {
    ... //Do something here
    return $result;
}
| improve this answer | |
  • Could you explain a bit more? What will the plugin of cache will do in my case? – anujeet Jun 11 at 14:16
  • this way you can check your conditions and perform the redirect before or after loading the content from cache. As you noticed that the event you are targeting doesn't get dispatched when pulling the page content from the cache. – Shapeshifter Jun 11 at 14:32
  • Will this work for Varnish as well? – anujeet Jun 12 at 8:03

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.