2

I want to create password protected page. Where users once click on password protected page then they redirect to the password page. And then after filling password, they redirect on actual page content. manage session and all.

| improve this question | | | | |
  • Please specify Magento version in your question. – Mohit Kumar Arora Jun 11 '18 at 6:23
  • Version: Magento 2.2.3 – Ravi Soni Jun 11 '18 at 6:25
0

There are many ways to do so. I would recommend not to override the controller action. I would rather prefer to use either Magento 2 plugin feature or use events and observers method.

If you want to use Events and Observers, then you can observe the event:\

controller_action_predispatch_cms_page_view'

or

controller_action_predispatch (If you want to cover more actions rather than only cms pages)

and then write your conditional redirection to the login page.

You can take help from this tutorial post.

| improve this answer | | | | |
0

To make the cms pages, you have to use event/observer

controller_action_predispatch_cms_page_view

ON this event,fire an observer which will check current user is loggedin or not. If user is not loggedin this redirect to login page.

events.xml define at app/code/{vendorname}/{ModuleName}/etc/frontend/ at code is like:

<?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="{VendorName}\{Modulename}\Observer\RestrictCmsPage" />
    </event>       
</config>

Observer file RestrictCmsPage.php located at app/code/{vendorname}/{ModuleName}/Observer/ Observer code like

<?php
namespace {VendorName}\{Modulename}\Observer;

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

class RestrictCmsPage implements ObserverInterface
{
    /**
     * @var \Magento\Framework\App\ActionFlag
     */
    protected $_actionFlag;
    /**
     * @var \Magento\Customer\Model\Session
     */

    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;
    }
}
| improve this answer | | | | |
0

There are two way.

First

Make CMS pages available only to logged in customers

Reference 1

Reference 2

You can use this code in any phtml file to redirect to login page:

 <?php 
        $obm = \Magento\Framework\App\ObjectManager::getInstance();
        $redirect = $obm->get('\Magento\Framework\App\Response\Http');
        $context = $obm->get('Magento\Framework\App\Http\Context');
        $cmsPage = $obm->get('\Magento\Cms\Model\Page');

        $pageIdentifier = $cmsPage->getIdentifier(); // CMS Page Identifier

        $isLoggedIn = $context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH);

        if($pageIdentifier == 'cms-page-identifier' && !$isLoggedIn)
        {
           $redirect->setRedirect('customer/account/login/');
        }
 ?>

Second

I am not 100% convinced its secure but seems to block the CMS page unless you have entered the right password. Add this to the top of your CMS page content:

<script type="text/javascript">
    var password = 'password'
    var retVal = prompt("Enter your password : ", "your password here");
    if(retVal !== password)
    {
        location.assign('#');
    }
</script>
| improve this answer | | | | |

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.