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.
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:
Step 2: Lastly, create the “Restrictcmspage.php” file inside the following path:
use Magento\Customer\Model\Session as CustomerSession;
class Restrictcmspage implements ObserverInterface
public function __construct(
$this->customerSession = $customerSession;
$this->_actionFlag = $actionFlag;
$this->redirect = $redirect;
public function execute(Observer $observer)
$this->_actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
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 ?