0
votes

i have a LoginController like this:

public function loginAction(){      
    $db = $this->_getParam('db');
    $this->_helper->viewRenderer->setNoRender();
    $this->_helper->getHelper('layout')->disableLayout();
    $adapter = new Zend_Auth_Adapter_DbTable(
            $db,
            'user',
            'username',
            'password',
            'MD5(CONCAT(?,password_salt))'
    );      
    $adapter->setIdentity($this->_request->getParam('username'));
    $adapter->setCredential($this->_request->getParam('password'));
    $auth = Zend_Auth::getInstance();  
    $result = $auth->authenticate($adapter);            
    if ($result->isValid()) {           
        // get all info about this user from the login table ommit only the password, we don't need that
        $userInfo = $adapter->getResultRowObject(null, 'password');
        $users = new Application_Model_DbTable_Users();
        $users->updateLastlogin($userInfo->email);          
        $auth->setStorage(new Zend_Auth_Storage_Session('testzf'));
        $authStorage = $auth->getStorage();
        $authStorage->write($userInfo);         
        $data = array('login'=>'success');              
    }

and a ProfileController:

public function getprofileAction(){
    $this->_helper->viewRenderer->setNoRender();
    $this->_helper->getHelper('layout')->disableLayout();       
    if(Zend_Auth::getInstance()->hasIdentity()) {   
        $username=$this->_request->getParam('username');
        $db_users = new Application_Model_DbTable_Users();
        $user = $db_users->getUser($username);

}

i made AjaxCalls for both Login and getprofile actions. I can login but getprofile doesn't work because Zend_Auth::getInstance()->hasIdentity() returns null. I see 2 session files in the folder as in application.ini. resources.session.save_path = APPLICATION_PATH "/../data/sessions" First one is full of session data, the second one is empty 0KB. Should this work through Ajax-Calls or i make an Error?

Regards

2

2 Answers

1
votes

Since you've used a custom auth storage key in your login action (testzf), you'll need to set this whenever you want to access the auth data:

public function getprofileAction(){
    $this->_helper->viewRenderer->setNoRender();
    $this->_helper->getHelper('layout')->disableLayout();       

    $auth = Zend_Auth::getInstance();
    $auth->setStorage(new Zend_Auth_Storage_Session('testzf'));

    if($auth->hasIdentity()) {   
        $username=$this->_request->getParam('username');
        $db_users = new Application_Model_DbTable_Users();
        $user = $db_users->getUser($username);
    }
}
0
votes

Please use Zend_Auth_Storage_Session like

in your login action

// check ajax request

$result = array(
            'error' =>  true,
            'msg'   =>  'Something went wrong,please try again!'
        );

if(!$this->_request->isXmlHttpRequest()){
 $this->_helper->json->sendJson($result);
}
                /**
                 * user authentication
                 */                 
                $authAdapter    = new Zend_Auth_Adapter_DbTable(null,$this->_table_user);
                $authAdapter->setIdentityColumn('user_name')
                            ->setCredentialColumn('password');
                $authAdapter->setIdentity($user_name)
                            ->setCredential($password);
                $select = $authAdapter->getDbSelect();
                $select->where('status = ?',1);
                $result = $authAdapter->authenticate($authAdapter);

                /**
                 * user validate
                 */ 

                if($result->isValid()){                 
                    $userStorage = new Zend_Auth_Storage_Session('Zend_Auth_User','User_Storage');
                    $userData = $authAdapter->getResultRowObject();
                    $userStorage->write($userData );

                    $this->_helper->json->sendJson(array('error'=>false));      
                }

// in your profile action

/** * you will get user session data through Zend_Auth_Storage_Session * you can write this function in action controller you will get in your controller easily like $this->_helper->json->sendJson(array('error'=>false)); */

$userStorage = new Zend_Auth_Storage_Session('Zend_Auth_User','User_Storage');      
$userData    = $userStorage->read();

suppose you need to check hasIdentity() you should use default Zend_Auth_Storage_Session like $userStorage = new Zend_Auth_Storage_Session(); // default namespace for auth storage (with out any namespace)

you used any namespace for in Zend_Auth_Storage_Session like Zend_Auth_Storage_Session('testzf') you need to set storage in $auth instance like

$auth = Zend_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_Session('testzf'));
$auth->hasIdentity(); // you will get storage here