3

I am trying to redirect to user after login or creating account to either home page or previous page. My controller code is following it is not able to login or create an account.

<?php
/**
 *
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace PinkBlue\Redirect\Controller\Index;

use Magento\Customer\Model\Registration;
use Magento\Customer\Model\Session;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Context;

class Redirect extends \Magento\Customer\Controller\AbstractAccount
{
    /** @var Registration */
    protected $registration;

    /**
     * @var Session
     */
    protected $session;

    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Context $context
     * @param Session $customerSession
     * @param PageFactory $resultPageFactory
     * @param Registration $registration
     */
    public function __construct(
        Context $context,
        Session $customerSession,
        PageFactory $resultPageFactory,
        Registration $registration
    ) {
        $this->session = $customerSession;
        $this->resultPageFactory = $resultPageFactory;
        $this->registration = $registration;
        parent::__construct($context);
    }

    /**
     * Customer register form page
     *
     * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        if ($this->session->isLoggedIn() || !$this->registration->isAllowed()) {
            /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
            $resultRedirect = $this->resultRedirectFactory->create();
            $resultRedirect->setPath('*/*');
            return $resultRedirect;
        }
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        //$currentUrl = $objectManager->get('Magento\Framework\UrlInterface')->getCurrentUrl();
        $catalogSession = $objectManager->create('Magento\Catalog\Model\Session');
        $previousUrl = $this->_redirect->getRefererUrl();
        $catalogSession->setCustomerRedirectUrl($previousUrl);


        /** @var \Magento\Framework\View\Result\Page $resultPage */
        $resultPage = $this->resultPageFactory->create();
        return $resultPage;
    }
}
1

3 Answers 3

3

You can disable the redirect.

  1. On the Admin sidebar, tap Stores. Then under Settings, choose Configuration.
  2. In the panel on the left, under Customers, choose Customer Configuration.
  3. Expand the Login Options section.
  4. Set Redirect Customer to Account Dashboard after Logging in to one of the following:

    • Yes - The account dashboard appears when customers log in to their accounts.
    • No - Customers can continue shopping after logging in to their accounts.
  5. When complete, tap Save Config.

  6. Click the Cache Management link in the yellow modal that shows up.

enter image description here

  1. Select the Configuration and Page Cache checkboxes.

enter image description here

  1. Click Submit to refresh the two selected cache types.

enter image description here


Magento Docs are your friend.

Cheers!

7
  • I have already tried to do the same but it was not working in my system I tried in other system it worked but behaving different sometime again tried after clearing system history its working fine Now!! Thanks!! Aug 19, 2016 at 5:33
  • After Login it is redirecting to home page but that is not logged In, again when I click to Sign In /Create it is redirecting to Dashboard. :( Aug 19, 2016 at 5:45
  • I'm not sure what you mean when you said "but that is not logged in". Did you clear the cache afterwards? I'll update my answer to reflect having to clear the cache. Aug 19, 2016 at 6:05
  • I mean ''but that is not logged In" this that is not showing User name in header still having Sign In /Create option for logged In user. is there any session issue? Aug 19, 2016 at 6:14
  • I updated my answer to include refreshing the cache. Are you still having issues after completing those steps? If so, then there is something else going on. Start by looking at the log files in the var/log directory. And if you possibly have another module that you were developing still enabled, don't forget to disable/remove it. Aug 19, 2016 at 6:31
2

You can also apply below solution.

In your custom theme phtml file , for login link, add below code.

$currentUrl  = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$login_url = $this->getUrl('customer/account/login', array('referer' => base64_encode($currentUrl)));

May this will help you.

Thank you

1
  • yes this is working , but when we can search product list of any category and then click on login button then it give 404 error i think it not get paramters from url. Give me any solution Feb 24, 2020 at 7:25
0

You can try to use in controller:

return $this->_redirect($previousUrl);

Also check the value in $previousUrl, it could be blank.

In order your login could work correctly with the full page cache - do not forget to flush the customer data in the browser local storage after registration or login.

We use the same logic in our Twitter & Facebook Login Free extension for Magento.

Hope it will help you.

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.