we made a totally custom login page in our Magento 2.2

on form submit, we send the credentials to a custom php page.

In the custom page we get $_POST['email'] and $_POST['password'].

How to trigger the magento customer login in this custom PHP script?

We tried some, but without success...

  • you cannot create a custom php file you create a custom Module For that – Waqar Ali Jul 31 '19 at 9:08
  • Have you any suggestion, guide or reference to do this? – Francesco Mineo Jul 31 '19 at 9:27

Please try with below code to login with email.

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$obj = $bootstrap->getObjectManager();

$state = $obj->get('Magento\Framework\App\State');

// Load customer By Email
$customer = $obj->create('Magento\Customer\Model\Customer')->loadByEmail('test@example.com');

// Create customer session
$customerSession = $obj->create('Magento\Customer\Model\Session');

if($customerSession->isLoggedIn()) {
    echo "Customer Logged in Successfully";
    echo "Unable to Login";

i hope its work for you.

|improve this answer|||||
  • This seems to work but not quite... The script return "Customer Logged in Successfully", but after that I cannot navigate to my account section, I am always redirected to login page... – Francesco Mineo Jul 31 '19 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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