I'm trying to login a user by ID but having issues with them staying logged in.
The user gets redirected to their account page but as soon as they click any link, they're not logged in. I am seeing a frontend cookie being set as well.
I am extending the Account Controller and here is my loginPostAction() method. Everything works except for the session.
require_once('Mage/Customer/controllers/AccountController.php');
class Silk_Casauth_AccountController extends Mage_Customer_AccountController {
/**
* Checks if the customer is logged into CAS, if not takes them to login.
*/
public function loginAction() {
if ($this->_getSession()->isLoggedIn()) {
$this->_redirect('*/*/');
return;
}
if(phpCAS::isAuthenticated()) {
$this->loginPostAction();
} else {
phpCAS::forceAuthentication();
}
}
/**
* Customer logout action
*/
public function logoutAction() {
$this->_getSession()->logout()->setBeforeAuthUrl(Mage::getUrl());
phpCAS::logout();
}
/**
* Login post action
*/
public function loginPostAction() {
// Customer is logged in so lets redirect them to where the need to go
if ($this->_getSession()->isLoggedIn()) {
$this->_redirect('*/*/');
return;
}
unset($_COOKIE['session_for%3Aindex_php']);
$r = $this->getRequest()->getParam('r');
$sid = $this->getRequest()->getParam('SID');
$session = $this->_getSession();
$email = phpCAS::getUser();
$uuid = KomApi::api()->get_user_by_email($email)->uuid;
// Looking up the user account in Magento
$customer = Mage::getModel('customer/customer')->getCollection();
$customer->addAttributeToSelect('uuid')->addAttributeToFilter('uuid', $uuid)->load()->getFirstItem()->getData();
foreach ($customer as $cust) {
$customer_id = $cust->getId();
}
if ($customer_id) {
// Lets update the customer data
$customer = Mage::getModel('customer/customer')->load($customer_id);
$customer->set_name_email_from_kvo($uuid);
} else {
// This is a new customer in Magento so lets create the account
$customer = Mage::getModel('customer/customer')->setId(null);
$customer->create_user_from_kvo($uuid);
$customer_id = $customer->getId();
// Show the welcome message the first time a customer logs in
$this->_welcomeCustomer($session->getCustomer(), true);
}
$customer = Mage::getModel('customer/customer')->load($customer_id);
$customer->delete_all_addresses();
$customer->create_addresses_from_kvo($uuid);
// Log the customer in
$session->setCustomerAsLoggedIn($customer);
// Redirect back through this loop to be caught at the begining of the method
$this->_loginPostRedirect();
}
/**
* Customer create redirect to club
*/
public function createAction() {
$this->_redirectUrl(Mage::getModel('core/variable')->loadByCode('club_base_uri')->getValue('plain') . "/signup/?r=" . Mage::getBaseUrl() . "customer/account/");
}
}
When I dump $session->loginById($customer_id)
it returns true so it seems that part is working.
Edit with config.xml
<?xml version="1.0"?>
<config>
<modules>
<Silk_Casauth>
<version>0.1.0</version>
</Silk_Casauth>
</modules>
<frontend>
<routers>
<customer>
<args>
<modules>
<Silk_Casauth before="Mage_Customer">Silk_Casauth</Silk_Casauth>
</modules>
</args>
</customer>
</routers>
</frontend>
<global>
<models>
<customer>
<rewrite>
<customer>Silk_Casauth_Model_Customer</customer>
</rewrite>
</customer>
</models>
</global>
</config>
I did some more testing and found that the default Magento login wasn't working either so I started messing with my config.xml file for this and found that if I remove frontend and everything in it that the default Magento login works correctly.
I have looked through my code and other examples and from what I gather I have everything correct. I'm not sure where to go from here.
EDIT: It doesn't look like it's the config.xml file. When I made my changes to the core controller I was getting the same issue. I have updated above with the entire controller that I'm using.
More digging and I found in Magento's system.log Warning: include(CAS/Client.php): failed to open stream: No such file or directory in /var/www/domain.com/app/lib/Varien/Autoload.php on line 93
. So it seems that when I inlude the files above the class that Magento's autoloader is trying to load CAS but the path is wrong. I'm not sure how to go about this now.
Latest Edit: I've required the CAS files in Magento's index.php file and that seems to have solved the issue but seems like a really dirty way of doing it.
$this->_getSession()->logout()->setBeforeAuthUrl(Mage::getUrl());