I have a process in Magento 1 to log user in using the below;

$session = Mage::getSingleton('customer/session');

$email = $this->getRequest()->getParam('email');
$password = $this->getRequest()->getParam('password'); 

//log user in
$session->login($email, $password);

//then check session/is logged in
if (Mage::getSingleton('customer/session')->isLoggedIn()) {

  //successfully logged in user


I wish to process the same in Magento 2, but unsure if $session->login() can be executed or is there another function in:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');

//or Magento equivalent for below;
$session = Mage::getSingleton('customer/session');

2 Answers 2


I hope this will help you.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
// Load customer
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load(2); //2 is Customer ID

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

if($customerSession->isLoggedIn()) {
    echo "Customer Logged in";
    echo "customer is Not Logged in";
  • 1
    Although not entirely what I was after, pointed me in the right direction and answers the question. thank you +1
    – BENN1TH
    Sep 15, 2018 at 10:18

See below solution for when both email & password are passed for validation/session authentication ...

Uses EncryptorInterface to validate passed password against users password hash

$this->_encryptor->validateHash($password, $sessionPasswordHash);

Full code below, but excludes additional checks that user is infact logged in after $this->_customerSession->setCustomerAsLoggedIn($customer) or before processing request checks if (!empty($email) && !empty($password)){ ... }


namespace My\Module\Controller\Api;
use Magento\Framework\App\Action\Context;

class Auth extends \Magento\Framework\App\Action\Action

protected $jsonResultFactory;
protected $_customer;
protected $_customerSession;

protected $_encryptor;

public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\Controller\Result\JsonFactory $jsonResultFactory,,
    \Magento\Customer\Model\Customer $customer,
    \Magento\Customer\Model\Session $customerSession,
    \Magento\Framework\Encryption\EncryptorInterface $encryptor
) {

    $this->_customer = $customer;
    $this->_customerSession = $customerSession;
    $this->_encryptor = $encryptor;

    $this->jsonResultFactory = $jsonResultFactory;


public function execute()
    $jsonResultArray = array();

    $email = $this->getRequest()->getParam('email');
    $password = $this->getRequest()->getParam('password'); 

    if (!empty($email) && !empty($password)){

                $customer = $this->_customer->loadByEmail($email);

                $sessionEmail = $customer->getEmail();
                $sessionPasswordHash = $customer->getPasswordHash();

                if($sessionEmail == $email && $this->_encryptor->validateHash($password, $sessionPasswordHash)){

                    $logUserIn = $this->_customerSession->setCustomerAsLoggedIn($customer);


                        $jsonResultArray['result'] = 'success';
                        $jsonResultArray['message'] = 'Successfully API Authenticated';


                        $jsonResultArray['result'] = 'error';
                        $jsonResultArray['message'] = 'Error with API Authentication';
                        $jsonResultArray['error'] = 'Error logging User In';



                    $jsonResultArray['result'] = 'error';
                    $jsonResultArray['message'] = 'Error with Authentication';
                    $jsonResultArray['error'] = 'Error logging User In';



        $jsonResultArray['result'] = 'error';
        $jsonResultArray['message'] = 'Error Authenticating';


    $result = $this->jsonResultFactory->create();

    return $result;


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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