Ever thought if you can log into Magento 2 admin without a password? Maybe you really want to do this or want to try it as an experiment. You can use a root PHP script and log into Magento 2 admin by using the username only.
You can write a custom root script to retrieve the admin user data from the UserFactory and authenticate the session. In this tutorial, I will show you how to do this, along with the fully working root script to log into Magento 2 admin without a password.
Magento Admin Login Without Password With Root Script
Here are the complete steps to admin login with root script in Magento 2 without using a password:
Step 1: Navigate to your Magento 2 root directory (where the index.php file is present) and create a new file named adminLogin.php.
Step 2: Copy the following script and paste it into the adminLogin.php file. Make sure to replace the $username = “admin” value in the script with the actual admin username in the store.
<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/../app/bootstrap.php';
/* code for dispaly error */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
class adminLoginApp extends \Magento\Framework\App\Http implements \Magento\Framework\AppInterface {
public function launch()
{
$areaCode = 'adminhtml';
$username = 'admin'; // admin user name '{username}'
$this->_request->setPathInfo('/admin'); // magento admin path exam. example.com/admin
$this->_state->setAreaCode($areaCode);
$this->_objectManager->configure($this->_configLoader->load($areaCode));
$user = $this->_objectManager->get('Magento\User\Model\User')->loadByUsername($username);
$session = $this->_objectManager->get('Magento\Backend\Model\Auth\Session');
$session->setUser($user);
$session->processLogin();
if($session->isLoggedIn()) {
$remoteAddress = $this->_objectManager->get('Magento\Framework\HTTP\PhpEnvironment\RemoteAddress');
$adminSessionInfo = $this->_objectManager->create('Magento\Security\Model\AdminSessionInfo');
$adminSessionInfo->setData('session_id', $session->getSessionId());
$adminSessionInfo->setData('user_id', $user->getUserId());
$adminSessionInfo->setData('ip', $remoteAddress->getRemoteAddress());
$adminSessionInfo->setData('status', '1');
$adminSessionInfo->save();
$cookieManager = $this->_objectManager->get('Magento\Framework\Stdlib\CookieManagerInterface');
$cookieValue = $session->getSessionId();
if ($cookieValue) {
$sessionConfig = $this->_objectManager->get('Magento\Backend\Model\Session\AdminConfig');
$cookiePath = str_replace('autologin.php', 'index.php', $sessionConfig->getCookiePath());
$cookieMetadata = $this->_objectManager->get('Magento\Framework\Stdlib\Cookie\CookieMetadataFactory')
->createPublicCookieMetadata()
->setDuration(3600)
->setPath($cookiePath)
->setDomain($sessionConfig->getCookieDomain())
->setSecure($sessionConfig->getCookieSecure())
->setHttpOnly($sessionConfig->getCookieHttpOnly());
$cookieManager->setPublicCookie($session->getName(), $cookieValue, $cookieMetadata);
}
$backendUrl = $this->_objectManager->get('Magento\Backend\Model\UrlInterface');
$path = $backendUrl->getStartupPageUrl();
$url = $backendUrl->getUrl($path);
$url = str_replace('adminLogin.php', 'index.php', $url); // adminLogin.php script file name
header('Location: '.$url);
exit;
}
return $this->_response;
}
}
$bootstrap = Bootstrap::create(BP, $_SERVER);
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
/** @var \Magento\Framework\App\Http $app */
$app = $bootstrap->createApplication('adminLoginApp');
$bootstrap->run($app);
Step 3: Now, you can use the command line to run the script or directly access the script in the browser and Magento 2 admin login using the root script.
If the file name you saved is adminLogin.php, then go to:
https://your-website.com/adminLogin.php
You’ll be successfully logged into the Magento admin panel using the username provided in the root script. This method is useful when you want to log into the admin panel, but have forgotten the password. However, it should be used with caution since this method can pose security issues.
Thanks for reading till the end. I hope this tutorial has helped you with Magento 2 login to the admin panel without password. In case of any queries, feel free to ask me in the comments below.