Magento2 admin login programmatically

Adobe commerce admin login programmatically

Sometimes we need Magento2 admin login programmatically.
If you forgot the password of the admin and you have a username then you create an Admin.php file and paste the below code.

After creating the file put it into the website running directory like pub.
Now hit the file like http://example.com/Admin.php

Code of the Admin.php file

<?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 = 'ib_admin'; // admin user name, Change it accordingly

		$this->_request->setPathInfo('/admin_ib'); // magento admin path exam. example.com/admin_ib, Change it accordingly
		$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);

Customer login programmatically in magento 2

Like us on Facebook and Linkedin for more updates.

Magento2 admin login programmatically

Note : Update “ib_admin” and “admin_ib” as your need.

Back To Top