bitExpert/magento2-force-login

Call to undefined method setUrl on Forward class

tzyganu opened this issue · 5 comments

Preconditions

  1. Magento Commerce version 2.3.3 (I think it reproduces on Open Source)
  2. Module version 4.0.0
  3. Value for customer/startup/redirect_dashboard config setting is 0.

Steps to reproduce

  1. Be logged out
  2. Add a product to wishlist
  3. I get redirected to login
  4. I login using the right credentials

Expected result

  1. I am logged in
  2. My product is added to the wishlist

Actual result

  1. I get an error call to undefined method setUrl on Magento\Framework\Controller\Result\Forward\Interceptor
  2. The error originates in AfterLoginPlugin https://github.com/bitExpert/magento2-force-login/blob/master/Plugin/AfterLoginPlugin.php#L85 $resultRedirect->setUrl($targetUrl);

Additional info

On the class Magento\Wishlist\Controller\AbstractIndex there is a plugin Magento\Wishlist\Controller\Index\Plugin that sets some redirect data in session if the customer is not logged in

            $this->customerSession->setBeforeWishlistRequest($data);
            $this->customerSession->setBeforeRequestParams($this->customerSession->getBeforeWishlistRequest());
            $this->customerSession->setBeforeModuleName('wishlist');
            $this->customerSession->setBeforeControllerName('index');
            $this->customerSession->setBeforeAction('add');

The LoginPost controller returns $this->accountRedirect->getRedirect(); (which is the second parameter to the AfterLoginPlugin::afterExecute method in this module).
In tthe above mentioned getRedirect method, in case this->session->getBeforeRequestParams() returns something, then an instance of \Magento\Framework\Controller\Result\Forward is returned and this causes an error in your plugin class because the Forward class does nto have a method called setUrl.

What I did to overcome this is to replace $resultRedirect->setUrl($targetUrl); in your plugin class with

if ($resultRedirect instanceof \Magento\Framework\Controller\Result\Redirect) {
    /** @var $resultRedirect Redirect */
    $resultRedirect->setUrl($targetUrl);
}

This seems to solve the problem but I'm not sure it's the way to go.

Thanks for raising this issue. We'll have a look as soon as we can. @websharp maybe you have an opinion on this matter :)

@shochdoerfer I can have a look, and think of the workflow if it not just a Redirect directive. Thank you @tzyganu for reporting.

@tzyganu I added your provided solution to the master, would you mind to check, if this works for you?

@websharp thanks for the fix. Will try it as soon as possible

I assume the issue is fixed already. I could not reproduce it with the dev-master version of this module and Magento 2.4.4. If you feel there's more we need to fix, let us know.

The next version of the module will be released in next few days.