bitExpert / magento2-force-login

Force Customer Login Module for Magento 2
https://marketplace.magento.com/bitexpert-magento2-force-customer-login.html
Apache License 2.0
166 stars 73 forks source link

Call to undefined method setUrl on Forward class #195

Closed tzyganu closed 2 years ago

tzyganu commented 3 years ago

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.

shochdoerfer commented 3 years ago

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

der-workfloh commented 3 years ago

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

der-workfloh commented 3 years ago

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

tzyganu commented 3 years ago

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

shochdoerfer commented 2 years ago

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.