I need to logout the user after successful registration. How can I achieve this,
Is there a admin settings in backend to do this.
I need to logout the user after successful registration. How can I achieve this,
Is there a admin settings in backend to do this.
How about going in the Magento backend, and set up the account setting to require customer to confirm his email (see screenshot below)
with this setting, once your customer has created his account, he won't be logged in. The screen will be like below:
magento does have a setting like this. You have to do customization.
In this case, you have to use two events
First,customer_register_success and customer_data_object_login,
When Customer is register then customer_register_success fire first then
customer_data_object_login fire.
On customer_register_success event add a registry variable
and then customer_data_object_login event catch this registry and run logout function.
The use of Registry flag, to prevent logout function on every customer_data_object_login observer call.
As we know that customer_data_object_login is called when customer is loggeding.
Create events.xml at app/code/{Vendorname}/{Modulename}/etc/frontend:
and code is :
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* @author Amit Bera
* @copyright Copyright (c) 2018 amitbera.com (https://www.amitbera.com/)
*/
-->
<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="customer_register_success_set_flag"
instance="{Vendorname}\{Modulename}\Observer\customerRegisterSuccessObserver"/>
</event>
<event name="customer_data_object_login">
<observer name="customer_data_object_login_customer_logout"
instance="{Vendorname}\{Modulename}\Observer\CustomerDataObjectLoginObserver"/>
</event>
</config>
Set registry variable, so create an observer class
customerRegisterSuccessObserver.php at app/code/{Vendorname}/{Modulename}/Observer
And code is:
<?php
namespace {Vendorname}\{Modulename}\Observer;
class customerRegisterSuccessObserver implements \Magento\Framework\Event\ObserverInterface
{
/**
* @var \Magento\Framework\Registry
*/
private $registry;
public function __construct(
\Magento\Framework\Registry $registry
) {
$this->registry = $registry;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
if($this->registry->registry('customer_register')){
$this->registry->unregister('customer_register');
}
// register flag
$this->registry->register('customer_register', 'yes');
}
}
check registry variable and fire logout so create another observer class
CustomerDataObjectLoginObserver.php at app/code/{Vendorname}/{Modulename}/Observer
and code is:
<?php
namespace {Vendorname}\{Modulename}\Observer;
class CustomerDataObjectLoginObserver implements \Magento\Framework\Event\ObserverInterface{
/**
* @var \Magento\Customer\Model\Session
*/
private $customerSession;
/**
* @var \Magento\Framework\Registry
*/
private $registry;
public function __construct(
\Magento\Framework\Registry $registry,
\Magento\Customer\Model\Session $customerSession
) {
$this->registry = $registry;
$this->customerSession = $customerSession;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
/**
* Check variable and logout
*/
if($this->registry->registry('customer_register')
&& $this->registry->registry('customer_register') == 'yes'){
$this->customerSession->logout();
}
return $this;
}
}
Note that code is not test, post on base on idea.
customer_data_object_loginevent.
– Amit Bera
Jan 02 '19 at 10:56
customer_data_object_login should run.if the line L373 https://github.com/magento/magento2/blob/2.3-develop/app/code/Magento/Customer/Controller/Account/CreatePost.php#L373 code is executed then customer_data_object_login then this observet must.Please check the same on default magento
– Amit Bera
Jan 04 '19 at 08:36
It can be easily done by the following module.
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