How To

How to Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2

Hello Magento Friends ?,

Welcome to MageComp’s Technical Blog series. Today we are going to dig out How to Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2. Our previous blog was Solved: Export File gets “Message is added to queue, wait to get your file soon” in Magento 2.3.X.

Introduction:

You might have visited some sites that won’t allow you access to some page if you are not logged in. Magento 2 also facilitates to restrict users who are not registered. When the user seeks access to the CMS page of your Magento 2 store, you can redirect them to the login page. After the user login, it will be again redirected to the previous page.

Let’s see, How to Redirect Customer to Access CMS Page and Redirect to Login Page in Magento 2 ?

Steps to Restrict Customer to Access CMS Page and Redirect to Login Page in Magento 2:

Step 1: First, we need to create an “events.xml” file inside the following path:

app\code\Vendor\Extension\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_cms_page_view">
        <observer name="add_login_checker" 
        instance="Vendor\Extension\Observer\Restrictcmspage" />
    </event>       
</config>

 Step 2: Lastly, create the “Restrictcmspage.php” file inside the following path:

app\code\Vendor\Extension\Observer\Restrictcmspage.php

<?php
namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Customer\Model\Session as CustomerSession;

class Restrictcmspage implements ObserverInterface
{
   
    protected $_actionFlag;
   
    public function __construct(
        CustomerSession $customerSession,
        \Magento\Framework\App\ActionFlag $actionFlag,
        \Magento\Framework\App\Response\RedirectInterface $redirect
        )
    {
        $this->customerSession = $customerSession;
        $this->_actionFlag = $actionFlag;
        $this->redirect = $redirect;
    }
    public function execute(Observer $observer)
    {
        if (!$this->customerSession->authenticate()) 
       {

            $this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
            if (!$this->customerSession->getBeforeUrl()) 
            {
                $this->customerSession->setBeforeUrl($this->redirect->getRefererUrl());
            }
        }
        return  $this;
    }
}

That’s it.

Conclusion:

You have successfully implemented Restrict Customer to Access CMS Page and Redirect to Login Page 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.

Happy Restricting ?

Click to rate this post!
[Total: 15 Average: 4.5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 day ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

1 day ago

Magento 2: How to Add Header and Footer in Checkout

Hello Magento Friends, In today’s blog, we will discuss adding a header and footer to…

2 days ago

Understanding Flexbox Layout in React Native

Hello React Native Friends, Building a visually appealing and responsive mobile app is crucial in…

4 days ago

HYVĂ„ Themes Releases: 1.3.6 & 1.3.7 – What’s New

We're thrilled to announce the release of Hyvä Themes 1.3.6 and 1.3.7! These latest updates…

4 days ago

How Modern E-Commerce Platforms Leverage Docker & Kubernetes for Scalability

Your e-commerce platform is surging - orders are rolling in, traffic spikes are becoming the…

5 days ago