views:

23

answers:

1

In my entry controller I set:

This works:

$authadapter = new Test_Auth_Adapter_DbTable($db);
$auth = Zend_Auth::getInstance();

$result = $auth->authenticate($authadapter);
$data = $authadapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
$this->_redirector->gotoUrl('/main');

This does not:

$authadapter = new Test_Auth_Adapter_DbTable($db);
$auth = Zend_Auth::getInstance();

$auth->setStorage(new Zend_Auth_Storage_Session('Test_User')); //the only difference

$result = $auth->authenticate($authadapter);
$data = $authadapter->getResultRowObject(null, 'password');
$auth->getStorage()->write($data);
$this->_redirector->gotoUrl('/main');

I can see it set in the $_SESSION var with all of the correct data when I use a debugger but after the data is set and I redirect to the desired destination the $_SESSION var is no longer set thus I cannot access things!

The page being redirected to checks auth with:

$this->auth = Zend_Auth::getInstance();
        if (!$this->auth->hasIdentity()) {
            $this->_redirector->gotoUrl('/entry');
        }

Why doesn't this work?

+2  A: 

Try this:

$this->auth = Zend_Auth::getInstance();
$this->auth->setStorage(new Zend_Auth_Storage_Session('Test_User'));
if (!$this->auth->hasIdentity()) {
    $this->_redirector->gotoUrl('/entry');
}

the problem is if you don't set a storage class, it will default to using Zend_Auth_Storage_Session with the Zend_Auth namespace. And because your session data isn't in that namespace, Zend_Auth doesn't see it and behaves as if the user is not logged in.

Depending on how your application is structured (and how big a part of it the auth is), you might want to do this in a bootstrap method instead so you only have to do it once:

protected function _initAuth()
{
    $auth = Zend_Auth::getInstance();
    $auth->setStorage(new Zend_Auth_Storage_Session('Test_User'));

    return $auth;
}
Tim Fountain
That did it. I assumed that calling setStorage() in the login controller action would tell ZF that that particular namespace was one that existed. I didn't realize I had to tell it to write it and then manually tell it to check it as well (since it's automatic with the Zend_Auth I assumed it was automatic with any namespace that had been set).
rg88