Zend_Auth and Zend_Session_Namespace... Please Review.
Posted: Fri Jun 18, 2010 2:18 pm
So this code, feels like it works, but I'd like your input... perhaps it doesn't and I'm just messing with my own head... who knows... Anyways, if it looks fine, if you have any input on how to make it better please let me know... if it doesn't work like I obviously intended it to do, please help me fix it.
Bootstrap.php:
UserController.php:
Yes, I've googled, couldn't find any information on this topic... so weird too...
All i'm trying to do is keep the user logged in for a week and not get lost every time he navigates away from the page like normal Zend_Auth does.
Bootstrap.php:
Code: Select all
...
protected function _initSessionHelper()
{
Zend_Session::start();
$sess = new Zend_Session_Namespace('Default');
$auth = Zend_Auth::getInstance();
if($sess->user && !Zend_Auth::getInstance()->hasIdentity())
{
$authStorage = $auth->getStorage();
$authStorage->write($sess->user);
}
}
...
Code: Select all
...
public function signinAction()
{
if(Zend_Auth::getInstance()->hasIdentity()){
$this->_redirect('/');
}
$request = $this->getRequest();
$form = new Application_Form_Signin();
if($request->isPost()){
if($form->isValid($this->_request->getPost())) {
$username = $form->getValue('username');
$password = $form->getValue('password');
$authAdapter = new Application_Auth_Adapter($username,$password);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$user = $this->_usersMapper->findByUsername($username,new Application_Model_Users());
$sess = new Zend_Session_Namespace('Default');
$sess->user = $user;
Zend_Session::rememberMe(604800); // Week
$authStorage = $auth->getStorage();
$authStorage->write($user);
$this->_redirect('/');
} else {
$this->view->errorMessage = "User name or password is wrong.";
}
}
}
$this->view->form = $form;
}
...
public function signoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
Zend_Session::forgetMe();
Zend_Session::destroy(true);
$this->_redirect('/');
}
...
All i'm trying to do is keep the user logged in for a week and not get lost every time he navigates away from the page like normal Zend_Auth does.