How To

Magento 2: How to Disable Customer Redirect to Login, Register and Forgot Password using Observer

Hello Magento Friends,

In today’s blog, we will discuss how to disable customer redirect to login, register and forgot password using observer in Magento 2.

In Magento 2, managing customer redirects can be crucial for providing a smooth and secure user experience. Sometimes, you may want to disable redirects to certain pages like login, register, and forgot password.

Let’s find out how you can disable customer redirects to login, register and forgot password using observer in Magento 2.

Steps to Disable Customer Redirect to Login, Register and Forgot Password using Observer in Magento 2:

Step 1: Create events.xml file inside the Extension etc folder

app\code\Vendor\Extension\etc\

Now add the code as below

<?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="controller_action_predispatch_customer_account_login">
        <observer name="redirect_login" instance="Vendor\Extension\Observer\Redirectpages" />
    </event>
    <event name="controller_action_predispatch_customer_account_create">
        <observer name="redirect_create" instance="Vendor\Extension\Observer\Redirectpages" />
    </event>
    <event name="controller_action_predispatch_customer_account_forgotpassword">
        <observer name="redirect_forgotpassword" instance="Vendor\Extension\Observer\Redirectpages" />
    </event>
</config>

Step 2: Now, create the Redirectpages.php file inside the Extension Observer folder.

app\code\Vendor\Extension\Observer\

Then add the code as follows

<?php
namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Response\RedirectInterface;

class Redirectpages implements ObserverInterface
{
    protected $redirect;

    public function __construct(RedirectInterface $redirect)
    {
        $this->redirect = $redirect;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $controller = $observer->getEvent()->getControllerAction();
        $controller->getResponse()->setRedirect($this->redirect->getRefererUrl())->sendResponse();
        exit;
    }
}

Conclusion:

By following these steps, you can disable customer redirects to login, register, and forgot password pages using an observer in Magento 2. This approach helps you manage customer experience more effectively without modifying core Magento files, ensuring a maintainable and upgrade-safe solution.

If you have any questions or run into issues, feel free to leave a comment below. Subscribe to our blog for more Magento 2 tips and tutorials. For more customization requirements, Hire Magento Developer to enhance your store functionality.

Happy Coding!

Click to rate this post!
[Total: 1 Average: 1]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • The blog on disabling customer redirects using observers in Magento 2 offers a practical and technical solution for controlling user navigation. The detailed guide and code examples are incredibly useful for developers looking to customize login, registration, and password reset processes. The approach ensures a smoother user experience by preventing unnecessary redirects. Great job on providing clear, actionable insights for improving Magento 2 store functionality!

Recent Posts

Handling Forms and Data in Shopify Remix: useSubmit vs. useFetcher

In Shopify Remix, managing form submissions and data fetching is crucial for building interactive and…

12 hours ago

SEO and Digital Marketing for Magento Stores

When positioning oneself in the constantly developing field of internet sales, it is critical to…

16 hours ago

Emerging Shopify Trends That Student Entrepreneurs Should Know About

One major challenge student entrepreneurs encounter is difficulty balancing academics and business. Most find themselves…

16 hours ago

How to Setup Vite in Shopify Remix App?

In this article, we will learn how to set up Vite in the Shopify remix…

2 days ago

Magento 2: How to Add View Button in Admin Grid to Open a View Page in Slide Window

Hello Magento Friends, In Magento 2, customizations to the admin panel can significantly enhance the…

3 days ago

Magento 2: How to Observe the Multi-shipping Order Creation Event

Hello Magento Friends, Magento 2 provides a robust event-driven architecture that allows developers to observe…

6 days ago