0

The error message is not showing on Login Observer if the error occours.

observer file is :

<?php

namespace Orbitx\Emailblock\Plugin;

use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\UrlInterface;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\UrlFactory;
use Magento\Framework\Message\ManagerInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;

class RedirectCustomUrl
{
protected $resultFactory;
protected $url;
protected $_request;
protected $_response;
protected $urlModel;
protected $resultRedirectFactory;
protected $messageManager;
protected $scopeConfig;
protected $MessageManager;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    UrlInterface $url,
    UrlFactory $urlFactory,
    RedirectFactory $redirectFactory,
    \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    ManagerInterface $messageManager,
    ResultFactory $resultFactory,
    \Magento\Framework\Message\ManagerInterface $MessageManager,
    \Magento\Framework\App\ResponseFactory $responseFactory

)
{
    $this->_request = $context->getRequest();
    $this->MessageManager = $MessageManager;
    $this->_response = $context->getResponse();
    $this->url = $url;
    $this->resultFactory = $resultFactory;
    $this->urlModel = $urlFactory->create();
    $this->resultRedirectFactory = $redirectFactory;
    $this->scopeConfig = $scopeConfig;
    $this->messageManager = $messageManager;
    $this->_responseFactory = $responseFactory;
}

public function aroundExecute(\Magento\Customer\Controller\Account\LoginPost $subject, \Closure $proceed) {    
    $email = $subject->getRequest()->getPost('login')['username'];
    if ($email == 'abc@getnada.com') {
        $this->messageManager->addErrorMessage(
            'Registration is disabled for you email'
        );
        $defaultUrl = $this->urlModel->getUrl('*/*/login', ['_secure' => true]);
        /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();

        return $resultRedirect->setUrl($defaultUrl);

    }
    return $proceed();
}
}

the exception 'Registration is disabled for you email' is not showing on frontend and if i entered wrong email or password it is also not giving any exception like 'your account is disabled'.

Can anyone tell me how to show the error message.

Any help is appriciated.

2
  • 1
    $email variable not defined, you should load customer session and then get email of customer Aug 18 at 11:43
  • @nihalmalik thanks for your response updated the file please check.
    – Prits
    Aug 18 at 11:45
0

Create events.xml app/code/Vendor/Module/etc/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="customer_login">
        <observer name="customer_login_observer" instance="Vendor\Module\Observer\CustomerLoginTest" />
    </event> 
</config>

in your observer class app/code/Vendor/Module/Observer/CustomerLoginTest.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class CustomerLoginTest implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "Customer LoggedIn";
        $customer = $observer->getEvent()->getCustomer();
        echo $customer->getEmail(); //Get customer name
        //add your custom code here//
       
    }
}
1
  • Thanks for answer @nihal but i have got the email. Just the error message is not showing on front-end please read my question again.
    – Prits
    Aug 18 at 11:58

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.