Tuesday, Jul 19, 2022 / Magento / Surbhi Patel

How to Add Webpage(s) Restriction for Non-Login User in Magento 2

If you want to restrict website content then use the below event for restriction, this guide will be very useful for you.

app/code/VendorName/ModuleName/etc/events.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<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=”restrict_website” instance=”VendorName\ModuleName\Observer\RestrictWebsite” />    </event>

</config>

app/code/VendorName/ModuleName/Observer/RestrictWebsite.php

<?php

namespace VendorName\ModuleName\Observer;

use Magento\Customer\Model\Context;

use Magento\Framework\Event\Observer;

use Magento\Framework\Event\ObserverInterface;

class RestrictWebsite implements ObserverInterface {

    /**

     * RestrictWebsite constructor.

     */

    /**

     * Customer session

     *

     * @var \Magento\Customer\Model\Session

     */

    protected $_customerSession;

    protected $_state;

    public function __construct(

        \Magento\Customer\Model\Session $customerSession,

        \Magento\Framework\Event\ManagerInterface $eventManager,

        \Magento\Framework\App\Response\Http $response,

        \Magento\Framework\UrlFactory $urlFactory,

        \Magento\Framework\App\Http\Context $context,

        \Magento\Framework\App\ActionFlag $actionFlag,

        \Magento\Framework\App\State $state,

        \Magento\Framework\UrlInterface $urlInterface

    ) {

        $this->_customerSession = $customerSession;

        $this->_response = $response;

        $this->_urlFactory = $urlFactory;

        $this->_context = $context;

        $this->_actionFlag = $actionFlag;

        $this->_state = $state;

        $this->_urlInterface = $urlInterface;

    }

    /**

     * @param Observer $observer

     * @return void

     */

    public function execute(Observer $observer) {

        $request = $observer->getEvent()->getRequest();

        $isCustomerLoggedIn = $this->_context->getValue(Context::CONTEXT_AUTH);

        $actionFullName = strtolower($request->getFullActionName());

        $module = $request->getModuleName();

        $areaCode = $this->_state->getAreaCode();

        $currentUrl = $this->_urlInterface->getCurrentUrl();

        $routename = $request->getRouteName();

        if ($areaCode == ‘frontend’) {

            $allowedRoutes = [

                ‘customer_account_login’,

                ‘customer_account_loginpost’,

                ‘customer_account_create’,

                ‘customer_account_createpost’,

                ‘customer_account_index’,

                ‘customer_account_logoutsuccess’,

                ‘customer_account_confirm’,

                ‘customer_account_confirmation’,

                ‘customer_account_forgotpassword’,

                ‘customer_account_forgotpasswordpost’,

                ‘customer_account_createpassword’,

                ‘customer_account_resetpasswordpost’,

                ‘customer_section_load’

            ];

            if (!$this->_customerSession->isLoggedIn() && !in_array($actionFullName, $allowedRoutes)) {

                $this->_response->setRedirect($this->_urlFactory->create()->getUrl(‘customer/account/login’));

            }

        }

    }

}

You have successfully implemented Restrict Customer Non-Login User in Magento 2 and you are free to customize this code according to your need for fetching data. If you have any queries write down in the comment section below. Help your Magento friends with this by sharing the article with them.

Voila and it’s done. Please let us know if there is anything we missed. Waiting for your comments. Stay tuned.

divider
author
Surbhi Patel
under Magento
line

You have a visionWe know the way to get you there.