Friday 31 March 2017

Magento 2 cannot login on frontend

After installing Sample data on localhost, I was not able to login with the dummy user login.
Am using xampp on windows 7, and working on Chrome browser.

Recently I found I cannot add any products to cart and found a solution for it.

The same solution applies here. This issue is due to form key mismatch. As I suggested in previous post, this is recommended only for testing environment i.e only on your localhost.

Quick Fix.


Go to vendor/magento/module-customer/Controller/Account/LoginPost.php execute() method.
Comment out the first if condition which checks the session and posted form key.

// if ($this->session->isLoggedIn() || !$this->formKeyValidator->validate($this->getRequest())) {

      // /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */

      // $resultRedirect = $this->resultRedirectFactory->create();

      // $resultRedirect->setPath('*/*/');

      // return $resultRedirect;

// } 

Magento 2 cannot add products to cart

On localhost environment, there is this annoying issue on CHROME browser. When you add products to cart, you will see no errors displayed, but products will not be added to cart.

Few of the stack overflow post suggest to change the localhost URL to 127.0.0.1 OR adding a vhost entry to change the localhost url to something else. I have found a quick fix for this issue. This fix is not recommended for Production environment.  This is only for the developers who are working on their local environment.


Problem


There is a form_key mismatch. Form key which gets posted during add to cart action and the stored Session value form key is mismatched.


Quick fix.


Go to vendor/magento/module-checkout/Controller/Cart/Add.php execute() method.
Comment out the first if condition which checks the session and posted form key.

//if (!$this->_formKeyValidator->validate($this->getRequest())) {

        //return $this->resultRedirectFactory->create()->setPath('*/*/');

//}