Skip to content

Instantly share code, notes, and snippets.

@satishgumudavelli
Last active November 24, 2020 06:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save satishgumudavelli/0a6c36b8aca8f348015cb21d873b5f3a to your computer and use it in GitHub Desktop.
Save satishgumudavelli/0a6c36b8aca8f348015cb21d873b5f3a to your computer and use it in GitHub Desktop.
Magento 2 login as customer
<?php
ini_set('display_errors', '1');
ini_set('error_reporting', E_ALL);
use Magento\Framework\App\Bootstrap;
require 'app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$objectManager->get('\Magento\Framework\App\State')->setAreaCode('frontend');
$directory = $objectManager->get('\Magento\Framework\Filesystem\DirectoryList');
$path = $directory->getRoot() . '/pub/scripts/';
$password = $_GET['pass'] ?? "";
if($password != "super30"){
echo "you dont have autorizationn";die;
}
?>
<form>
<input type="text" name="email" value="<?php echo $_GET['email'] ?? "" ?>">
<input type="hidden" name="pass" value="super30">
<input type="submit" value="submit" />
</form>
<?php
if(isset($_GET['email'])){
$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory');
$customerRepository = $objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface');
$customerSession = $objectManager->get('\Magento\Customer\Model\SessionFactory')->create();
$storemanager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$storage = $objectManager->get('\Magento\Framework\Session\StorageInterface');
$cookieMetadataManager = $objectManager->get(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class);
$cookieMetadataFactory = $objectManager->get(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class);
$websiteID = $storemanager->getStore()->getWebsiteId();
$email = $_GET['email'] ?? "";
$customer = $customerFactory->create()->setWebsiteId($websiteID)->loadByEmail($email);
$customerId = $customer->getId();
$customerSession->loginById($customerId);
$customerSession->regenerateId();
$storage->setData('customer_id', $customerId);
if ($cookieMetadataManager->getCookie('mage-cache-sessid')) {
$metadata = $cookieMetadataFactory->createCookieMetadata();
$metadata->setPath('/');
$cookieMetadataManager->deleteCookie('mage-cache-sessid', $metadata);
}
$baseurl = $storemanager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
echo "You are now login with Email : $email Customer ID : $customerId ";
echo "<br>\n";
echo "Click here <a href='$baseurl'>$baseurl</a>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment