0

I need to logout a user after successful registration to login page.

I tried to redirect the user after registration by creating a observer customer_register_success but it dint work, When the observer is called the loggin action is not triggered so I added a redirection that will solve my problem but dint. its working as usual.

public function execute(Observer $observer)
{
      $this->messageManager->addErrorMessage(__('Your account is not approved.'));
      $this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/login')); 

}

I tried with this link belo but it only works with login page and not in registration process.

Magento 2: Get Customer data after login with observer

How do I Logout the user.

3 Answers 3

0

Try this

di.xml add

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

    <event name="customer_register_success">
        <observer name="custom_customer_created" instance="Custom\Module\Observer\CustomerEventCreatedObserver" />
    </event>

</config> 

public function execute(Observer $observer)
{
      $this->messageManager->addErrorMessage(__('Your account is not approved.'));
      $this->_response->setRedirect($this->_urlFactory->create()->getUrl('customer/account/logout')); 

}
1
  • Have the same set of coding its not working for me. facing the same issue. Its not redirecting it logs me in and takes me to home page.
    – fernandus
    Commented Dec 31, 2018 at 5:52
0

I used this extension to trigger after Registration https://github.com/php-cuong/magento2-redirect-customer it used customer_login observer.

I did my logic to logout and redirect.

public function execute(Observer $observer)
{
   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $customerSession = $objectManager->create('Magento\Customer\Model\Session');
   $customerSession->logout();

   if($this->uri->isValid($particular_page)) {
      $resultRedirect = $this->responseFactory->create();
      $resultRedirect->setRedirect($particular_page)->sendResponse('200');
      exit();
    }
}
0

It can be easily done by the following module.

https://github.com/amitshree/magento2-account-approval/blob/master/Plugin/Customer/Controller/Account/LoginPost.php

Done by 2 methods

1.Event customer_register_success

 public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $this->coreRegistry->register('is_new_account', true);
    }

2.Override Magento\Customer\Model\Account\Redirect

    $this->messageManager->getMessages(true);
    // Adding a custom message
    $this->messageManager->addErrorMessage(__('Your account is not approved. Kindly contact website admin for assitance.'));
    // Destroy the customer session in order to redirect him to the login page
    $this->session->destroy();
    /** @var \Magento\Framework\Controller\Result\Redirect $result */
    $result = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
    $result->setUrl($this->url->getUrl('customer/account/login'));
    return $result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.