Storing an object in a Zend_Auth session

I saw on our Zend Technologies page on Facebook a question on how to get a logged in user profile when using Zend_Auth.  The exact question was this:

hye guys, i need help in Zend … i m implmenting web based application using zend framework the version i am using is 1.9.5 but i got a problem, i dont know how to display a user profile once he or she login into the system? anybody can help me doing so? i tried googling but mostly the code there is for zend 1.5 i guess.. wish can get it done as soon as possible

For my own login on the site I have code that is very similar, and it's one of the reasons why I'm writing a lot of my own stuff here; for the purpose of examples.  The way I set and access my session is actually relatively simple.

$adapter = new Zend_Auth_Adapter_DbTable(
    $this->getInvokeArg('bootstrap')->getPluginResource('db')->getDbAdapter(),
    'user',
    'user_email',
    'user_password',
    'sha1(?)'
);
$adapter->setIdentity($form->getValue('user_email'));
$adapter->setCredential($form->getValue('user_password'));
$res = Zend_Auth::getInstance()->authenticate($adapter);
if ($res->isValid()) {
    
    $ut = new Model_DbTable_User();
    $user = $ut->find(
        $adapter->getResultRowObject()->user_key
    )->current();
    
    Zend_Auth::getInstance()->getStorage()->write(
        $user
    );

... etc/

}

Basically, what I do is create my adapter, set my identity and my credential and do my regular authentication.  However, once have authenticated I get a new user module, which is what all of the users are based off of and do a find on the primary key.  Then I take the Zend_Auth instance, get the storage and write the model instance to the storage of the Zend_Auth instance.  Then later on if I want to get any infomation I can just call Zend_Auth::getInstance()->getIdentity().

Done

Related posts

Leave a Comment