2
votes

I have a custom script, and I want to create a kind of single sign on with Magento for this.

I already found code to create a session and login as customer

$session = Mage::getSingleton( 'customer/session' );
try {
    $session->login( $username, $password ); 
....

But using this does not work

 Mage::getSingleton( 'admin/session' );//adminhtml does also not work

Basically, what I want is this:

  • Have users login to Magento admin from custom login page

  • Keep track of their newly created session

another option for me would be to have users login to Magento, and if they then go to my script, I will see that the user is logged in as Admin. I found code for this, but somehow this does not work if the user first goes to my script. Here is the code:

$session = Mage::getSingleton('core/session', array('name'=>'adminhtml'));
if(Mage::getSingleton('admin/session')->isLoggedIn()){
....

The code above works, if they user FIRSTLY login to magento. But if they go to my script first, then I see a cookie gets created with name adminhtml by this line of code:

Mage::getSingleton('core/session', array('name'=>'adminhtml'

So if a user logs into Magento after this, and then goes to my script, this cookie still exists, and checking that for logged in returns false, while another sessioncookie with same name is actually logged in.

Help me please! Thanks!

1

1 Answers

1
votes

Try this

 $session = Mage::getSingleton('admin/session');
$session->setIsFirstVisit(true);
$session->setUser($user);
$session->setAcl(Mage::getResourceModel('admin/acl')->loadAcl());
Mage::dispatchEvent('admin_session_user_login_success',array('user'=>$user));