PHPCodez

Search
Skip to content
  • Home
  • PHP
  • Functions
  • WordPress
  • Plugins
  • Joomla
  • Magento
  • Linux
  • My SQL
  • jQuery
  • Profile
  • comments
Magento

Magento 2 Force Customer Login

May 18, 2018 Pramod T P Leave a comment

It can be achieved with the help of an observer.

Follow the below steps to create an extension PHPCodez_Keeplogin for this task.

1) Declare a module.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="PHPCodez_Keeplogin" setup_version="0.0.1" />
</config>

2) Register a module.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'PHPCodez_Keeplogin',
__DIR__
);

3) Create frontend/event.xml file and add the below content.

<?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_postdispatch">
<observer name="Keeplogin_controller_action_postdispatch" instance="PHPCodez\Keeplogin\Observer\KeepCustomerLoginObserver" />
</event>
</config>

4) Create an observer KeepCustomerLoginObserver.php

<?php
namespace PHPCodez\Keeplogin\Observer;

use Magento\Framework\Event\ObserverInterface;

class KeepCustomerLoginObserver implements ObserverInterface {

private $customerUrl;
private $context;
private $contextHttp;

public function __construct(
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\App\Http\Context $contextHttp,
\Magento\Customer\Model\Url $customerUrl
) {
$this->context = $context;
$this->customerUrl = $customerUrl;
$this->contextHttp = $contextHttp;
}


public function execute(\Magento\Framework\Event\Observer $observer) {

$controller_name = $this->context->getRequest()->getControllerName();
$action_name = $this->context->getRequest()->getActionName();

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

if ($isLoggedIn) {
return $this;
}

if ($controller_name === 'account' && ($action_name === 'login' || $action_name === 'loginPost' || $action_name === 'forgotpassword'
|| $action_name === 'createpassword' || $action_name === 'create' )
) {
return $this;
}

$customer_login_url = $this->customerUrl->getLoginUrl();
$this->context->getResponse()->setRedirect($customer_login_url);

}
}

5) Issue the following commands.

$ php bin/magento indexer:reindex
$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile
$ php bin/magento setup:static-content:deploy -f
$ php bin/magento cache:flush
CustomerForceLoginMagento 2

Post navigation

Previous PostDisable Frontend Registration Magento 2Next PostMagento 2 Disable Add To Cart Customer Group

Beginners' Guide

Recent Posts

  • Public Cloud
  • Amazon Machine Image
  • Resource Tags
  • Instance user data
  • Instance metadata

Recent Comments

  • NickyOKiefer on comments
  • 검증사이트 on compare two strings – javascript
  • https://www.postonlineads.com/ on Install rar on ubuntu
  • farhangift on XAMP Change Localhost To Domain
  • دانلود بازی اندروید on Include a block in sidebar – Magento

Categories

  • .htaccess (32)
  • Ajax (4)
  • Apache (43)
  • AWS (9)
  • Cookie (4)
  • Database Model (9)
  • Design Pattern (9)
  • Filter (27)
  • General (182)
  • HTML (1)
  • IP.Board (1)
  • Javascript (60)
  • Jetty (3)
  • Joomla (17)
  • jQuery (63)
  • JSON (3)
  • JSP (1)
  • Linux (115)
  • Magento (363)
  • My SQL (72)
  • nginx (4)
  • OOPs (12)
  • Payment Gateway (1)
  • PHP (1,002)
  • Plugins (11)
  • Prototype (1)
  • session (5)
  • Socialengine (1)
  • SQL (69)
  • SVN (17)
  • Uncategorized (11)
  • Vbulletin (11)
  • Wordpress (89)
Proudly powered by WordPress