Magento 1.9 – Fix Auto Logout Issue After Redirect with setCustomerAsLoggedIn()

customermagento-1.9redirect

Freshly created and active user, after redirection to user cabinet logged out instantly:

                $this->_session = Mage::getSingleton('customer/session');
                $session = $this->_session;

                $customer = Mage::getModel('customer/customer')->setEmail($email)
                                                               ->save();
                // ^^^ successfully saves the customer entity

                $session->setCustomerAsLoggedIn($customer);

                // autoconfirm user
                if($customer->getConfirmation()!=null){
                    $customer->setConfirmation(null);
                }
                if($session->isLoggedIn()) {

                    Mage::app()->getFrontController()->getResponse()->setRedirect(Mage::getBaseUrl().'customer/account/');
                    return;
                }

it successfully redirects and shows the user cabinet and 'Log out' link is active, but if I open the magento front page in other tab of same browser it becomes clearly that customer is not logged in.
Where to debug ?
Thanks!

UPDATE

I am trying to login freshly created customer without password. Password attribute set to 'required'=>false in customer entity, via install script.

UPDATE 2

I was also forgot to $customer->save(); after $customer->setConfirmation(null); it is mandatory

Best Answer

The actual customer login works similarly to your code but that function sets the website ID, make sure you are doing this.

$customer = Mage::getModel('customer/customer')
        ->setWebsiteId(Mage::app()->getStore()->getWebsiteId())->setEmail($email)->save();

$session->setCustomerAsLoggedIn($customer);
Related Topic