0

I have a custom module index page where I'm creating a session for a particular user and redirecting to the home page.

/app/code/Login/Sso/Controller/Index/Index.php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load(8857);
$createCustomerSession = $objectManager->create('Magento\Customer\Model\Session');
$createCustomerSession->setCustomerAsLoggedIn($customer);

if ($createCustomerSession->isLoggedIn()) {
    print_r($createCustomerSession->getCustomerId());
}
        
print_r(' Logged in? ' . $createCustomerSession->isLoggedIn());exit;
$this->_redirect('/');

When I hit {BASE_URL}/index.php/login, It returns me the customerId and Logged in? 1 (i.e. the customer is logged in) but if I remove exit from the print statement, its redirecting to home page but the user is not logged in. I could see the Sign In and Create an Account buttons.

Customer is not logged in on any other pages too. How is the session getting cleared?

Your Answer

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

Browse other questions tagged or ask your own question.