1

I've been strugling with this a lot, I have to redirect the users to Home when they login to the site. I've been following solutions given in other questions about the same, but nothing works, here are some links I've been using:
setBeforeAuthUrl and setAfterAuthUrl
Redirect to product page from observer
Redirect user after login
Magento2: redirection from Observer
All those are rather old and I'm wandering if those answers are deprecated or something, I'm currently working in Magento 2.4.2.
I'm using the observer approach right now, and is not working, I have this configuration:
Redirect Customer to Account Dashboard after Logging in NO
This is my events.xml

<?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="customer_login">
        <observer name="customer_login_observer" instance="Vendor\Loginredirect\Observer\Redirect" />
    </event>
</config>

And this is my Observer:

<?php    
namespace Vendor\Loginredirect\Observer;
    
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\ObjectManagerInterface;
    
class Redirect implements ObserverInterface
{
    /**
    * @var \Magento\Framework\ObjectManagerInterface
    */
    protected $objectManager;
    
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManagerInterface;
    
    public function __construct(
        StoreManagerInterface $storeManagerInterface,
        ObjectManagerInterface $objectManager
    )
    {
        $this->storeManagerInterface = $storeManagerInterface;
        $this->objectManager = $objectManager;
    }
    
    public function execute(Observer $observer)
    {
        $redirectionUrl = $this->storeManagerInterface->getStore()->getBaseUrl();
        $this->objectManager->get('Magento\Customer\Model\Session')->setBeforeAuthUrl($redirectionUrl);
    }
}

I know the code in the observer is executing because I used some logs to test it, there are no errors in the logs, all appears ok, but if I login as a customer I get to the Account Dashboard anyway.
Is there something I'm overlooking here?

1 Answer 1

0

Please follow below step to redirect to homepage after Login

Step 1:

Create a di.xml file under the app/code/Vendor/Modulename/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Controller\Account\LoginPost">
        <plugin name="redirect_dashboard_url" type="Vendor\Modulename\Plugin\RedirectDashboardUrl" sortOrder="1"/>
    </type>  
</config>

Create a Plugin RedirectDashboardUrl.php in under app/code/Vendor\Modulename\Plugin

<?php

declare(strict_types=1);
namespace Vendor\Modulename\Plugin;    

class RedirectDashboardUrl
{
    /**
     * Summary of afterExecute
     * @param \Magento\Customer\Controller\Account\LoginPost $subject
     * @param mixed $result
     * @return mixed
     */
    public function afterExecute(
        \Magento\Customer\Controller\Account\LoginPost $subject,
        $result
    ) {
        $customUrl = 'cms/index/index';
        $result->setPath($customUrl);
        return $result;
    }
}

After run below command

PHP bin/magento setup:di:com
PHP bin/magento s:s:d -f
PHP bin/magento c:c

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.