0

I need to logout the user after successful registration. How can I achieve this,

Is there a admin settings in backend to do this.

2

How about going in the Magento backend, and set up the account setting to require customer to confirm his email (see screenshot below)

enter image description here

with this setting, once your customer has created his account, he won't be logged in. The screen will be like below:

enter image description here

  • that could be good idea ! – Amit Bera Jan 2 at 10:52
  • thanks Amit! good to e-meet you star of StackExchange :-) – Herve Tribouilloy Jan 2 at 10:54
  • After clicking the link in my email it logs me in, it should not do so. – fernandus Jan 2 at 18:05
  • Facing the same issue. I am working with Acount approval by admin using this github.com/amitshree/magento2-account-approval. This extension has logout when registration but its not working for me. magento.stackexchange.com/questions/256434/… – fernandus Jan 2 at 18:11
  • that is the same issue but with an additional module that I have not used before. I'd need to install the module and look at reproducing your issue. Unfortunately this is not something I have the time to look at. Apologies Fernandus – Herve Tribouilloy Jan 2 at 18:35
1

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.

  • I tried to redirect the customer in customer_register_success so that login wont happen it dint work I also tried this, magento.stackexchange.com/questions/131850/… – fernandus Jan 2 at 10:49
  • redirection is not the solution. You have to log out on customer_data_object_login event. – Amit Bera Jan 2 at 10:56
  • Kindly find this magento.stackexchange.com/questions/256159/… I posted this. – fernandus Jan 2 at 11:02
  • Its only working before logging in a new user. After clearing cache and when I register a user it logout the user and for second third ... it works perferct, If I login a user and come back and create a new user it's not working it takes me to home page. If I clear cache again it works. Not sure why its happening. – fernandus Jan 2 at 11:03
  • I will try in Logging out in customer_data_object_login now ASAP – fernandus Jan 2 at 11:03

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.