Magento redirect after login

Magento redirect after login

August 4, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento redirect after login

Redirecting Users After Login: A Comprehensive Guide for Magento

Hello everyone! Today we’re going to tackle an issue that many Magento developers face - how to redirect users to the page they were on after successfully logging in.

In your case, you want to direct users from the mini-login form straight back to the product page so they can hit the ‘like’ button immediately. Unfortunately, the built-in login functionality in Magento doesn’t handle this natively.

Understanding the Default Login Behavior

By default, when a user logs into their account through the mini-login.phtml file, Magento redirects them to the “my account” dashboard (customer/account/index). This is because the loginPost action specified in your form submission URL is directed towards this endpoint.

The default login URL looks something like this: /customer/account/login/

Redirecting Users After Login

To achieve your desired behavior, you need to override the loginPost action and implement redirection logic. Here’s a step-by-step guide:

Step 1: Create an Event Observer

Create a new observer file under your custom module or theme: /app/code/Vendor/Module/etc/events.xml

Add this code to listen for the controller_action_postdispatch_customer_account_loginpost event:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App etc/config.xsd">
  <event name="controller_action_postdispatch_customer_account_loginPost">
    <observer name="redirect_after_login" instance="Vendor\Module\Model\Observer" />
  </event>
</config>

Step 2: Create the Observer Class

Create a new file /app/code/Vendor/Module/Model/Observer.php and implement the execute method:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $controllerAction = $observer->getControllerAction();
    $redirectUrl = $this->_request->getParam('redirect_url');
    if ($redirectUrl) {
        return $controllerAction->getResponse()->setRedirect($redirectUrl);
    }
}

Step 3: Modify the Mini-Login Form

Update your mini-login.phtml file to include a hidden input field for the current page URL:

<form action="<?php echo str_replace('login', 'loginPost', $this->helper('customer')->getLoginUrl()) ?>" method="post">
    <input type="hidden" name="redirect_url" value="<?= $block->getRedirectUrl() ?>">
</form>

The getRedirectUrl function should be placed within your mini-login block class:

public function getRedirectUrl()
{
    return $this->_request->getFullActionName();
}

If you’re looking to delve deeper into Magento event observers or customizing the login process, I highly recommend checking out these resources from Laravel Company:

Conclusion

In this post, we’ve explored how to override Magento’s default login redirect behavior and implement custom redirection logic. By creating an event observer and modifying your mini-login form, you can now direct users back to the page they were on after a successful login.

Remember, always test your changes thoroughly in a development environment before deploying them to a live site. And as always, if you’re looking for expert guidance with Magento development projects or need further assistance, don’t hesitate to reach out to us at Laravel Company. Happy coding!

Catalog Security Extensions Themes

Our network