0

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

segarci
  • 739
  • 1
  • 11
  • 19
user2683906
  • 65
  • 1
  • 6

2 Answers2

1

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);
    }
}
Tim Fountain
  • 33,093
  • 5
  • 41
  • 69
0

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
raheem.unr
  • 797
  • 1
  • 7
  • 16
  • Hello Arumugam, thank you for answering. I'm not sure if i understand. How can i check if the user has identity after getting userStorage? $userStorage = new Zend_Auth_Storage_Session('Zend_Auth_User','User_Storage'); $userData = $userStorage->read(); – user2683906 Mar 14 '14 at 08:22
  • you can check it if(empty($userData)) and you can see the information print_r(userData); – raheem.unr Mar 15 '14 at 11:46
  • Thanks again. I tried this, no success. I think zend has a problem with android query, because the strorage is always empty. – user2683906 Mar 15 '14 at 13:28