0

I want to make user register and redirect to login page in Magento 2.4. User not automatic login.

I have created a plugin for that and created a file ( app/code/Registration/Redirect/Plugin/Customer/Account/CreatePost.php )

<?php

namespace Registration\Redirect\Plugin\Customer\Account;

class CreatePost
{

/**
 * @var \Magento\Framework\UrlInterface
 */
protected $url;

/**
 * @param \Magento\Framework\UrlInterface $url
 */
public function __construct(
    \Magento\Framework\UrlInterface $url
)
{
    $this->url = $url;
}

public function afterExecute(
    \Magento\Customer\Controller\Account\CreatePost $subject,
    $resultRedirect
) {
    $resultRedirect->setUrl($this->url->getUrl('customer/account/login'));
    return $resultRedirect;
}
}

How to make it not loggedin automatic only just register and redirect on login page. Any help is appriciated.

4
  • I think you need to use before OR around plugin to modify actual execute.Now you are using after plugin and customer is already register and login.
    – Pawan
    Jun 16 at 15:08
  • @Pawan will you please tell me how to do that i have see need to do some with $this->session->setCustomerDataAsLoggedIn($customer); line but how to do that will you please help.
    – Prits
    Jun 16 at 15:10
  • YES, you need to remove code $this->session->setCustomerDataAsLoggedIn($customer);.just copy whole function code and return after customer account create code i.e after dispatch customer_register_success event.
    – Pawan
    Jun 16 at 15:18
  • Did you checked my answer? Is it worked for you? Jun 17 at 12:57
1

Try with below code in your plugin

<?php

namespace Registration\Redirect\Plugin\Customer\Account;

use Magento\Customer\Controller\Account\CreatePost;
use Magento\Framework\Controller\Result\RedirectFactory;
use Magento\Framework\App\Response\RedirectInterface;
use Psr\Log\LoggerInterface;
use Magento\Customer\Model\Session;

class CreatePost
{
    /**
     * @var \Magento\Framework\Controller\Result\RedirectFactory
     */
    protected $resultRedirectFactory;

    /**
     * @var \Magento\Framework\App\Response\RedirectInterface
     */
    protected $redirect;

    /**
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $customerSession;

    /**
     * Connect constructor.
     * @param \Magento\Framework\Controller\Result\RedirectFactory $redirectFactory
     * @param \Magento\Framework\App\Response\RedirectInterface $redirectInterface
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magento\Customer\Model\Session $customerSession
     */
    public function __construct(
        RedirectFactory $redirectFactory,
        RedirectInterface $redirectInterface,
        LoggerInterface $logger,
        Session $customerSession
    ) {
        $this->resultRedirectFactory = $redirectFactory;
        $this->redirect = $redirectInterface;
        $this->logger = $logger;
        $this->customerSession = $customerSession;
    }

    /**
     * @param \Magento\Customer\Controller\Account\CreatePost $subject
     * @param $result
     * @return \Magento\Framework\Controller\Result\Redirect
     */
    public function afterExecute(CreatePost $subject, $result)
    {
        if ($this->customerSession->getRegisterSuccess()){
            $lastCustomerId = $this->customerSession->getCustomerId();
            $this->customerSession->logout()->setBeforeAuthUrl($this->redirect->getRefererUrl())
                ->setLastCustomerId($lastCustomerId);

            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath('customer/account/login');
            $result = $resultRedirect;
        }
        return $result;
    }
}
1
  • Thanks for the answer i have also done the same.
    – Prits
    Jun 18 at 4:47

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.