1

I am getting below error while running Login Authentication Code on. I am using zend framework & zend studio as IDE

A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.

Below is the code which i have written:

public function authAction(){
        $request    = $this->getRequest();
        $registry   = Zend_Registry::getInstance();
        $auth       = Zend_Auth::getInstance();

        $DB = $registry['zenddb']; //zenddb is database name

        $authAdapter = new Zend_Auth_Adapter_DbTable($DB);
        $authAdapter->setTableName('user');



        $authAdapter->setIdentityColumn('user_name');
        $authAdapter->setCredentialColumn('password');

        // Set the input credential values
        $uname = $request->getParam('user_name');
        $paswd = $request->getParam('password');
        $authAdapter->setIdentity($uname);
        $authAdapter->setCredential(md5($paswd));

        // Perform the authentication query, saving the result
        $result = $auth->authenticate($authAdapter);

        if($result->isValid()){
            $data = $authAdapter->getResultRowObject(null,'password');
            $auth->getStorage()->write($data);
            $this->_redirect('userpage');
        }else{
            $this->_redirect('login');
        }
    }
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Gaurav Pawar
  • 52
  • 2
  • 7

3 Answers3

1

This error usually occurs when you leave the credential fields blank in your form,

so zend rectifies is with a catch block which throws the error which you mentioned. Official Ticket

you can solve it by putting validation on field related to the empty fields. // e.g. 'required' => true

hope it helps.

Ronak K
  • 1,567
  • 13
  • 23
0
 public function loginAction()
    {
        $this->_helper->layout->setLayout('loginlayout');

                $request = $this->getRequest();
                $form    = new Application_Form_loginForm();
                $login="";

                if ($this->getRequest()->isPost()) {
                    if ($form->isValid($request->getPost())) {
                        $request    = $this->getRequest();
                        // Set the input credential values
                    $registry   = Zend_Registry::getInstance();
                $auth       = Zend_Auth::getInstance();


                $params = array('host'      =>'localhost',
                        'username'  =>'root',
                        'password'  =>'',
                        'dbname'    =>'zendDb'
                );


                $DB = new Zend_Db_Adapter_Pdo_Mysql($params);


                $authAdapter = new Zend_Auth_Adapter_DbTable($DB);

                $authAdapter->setTableName('user');
                $authAdapter->setIdentityColumn('user_name');
                $authAdapter->setCredentialColumn('password');
                $request    = $this->getRequest();
                // Set the input credential values
                $uname = $request->getParam('user_name');

                $paswd = $request->getParam('password');

                $authAdapter->setIdentity($uname);
                $authAdapter->setCredential(md5($paswd));

                $auth       = Zend_Auth::getInstance();
                // Perform the authentication query, saving the result
                $result = $auth->authenticate($authAdapter);

                if($result->isValid()){
                    $data = $authAdapter->getResultRowObject(null,'password');

                    $auth->getStorage()->write($data);
                    $login="";
                    $this->_redirect('database/user');
                }else{

                   $login="Invalid User Name or Password";

                    //$this->_redirect('database/login');
                }


                //return $this->_helper->redirector('auth');
            }
            }
            else {
                $login="";
            }

            $this->view->assign('title','Login');
            $this->view->assign('description',$login);
            $this->view->form = $form;

    }
Gaurav Pawar
  • 52
  • 2
  • 7
0
if ($this->getRequest()->isPost()) {
    if ($form->isValid($request->getPost())){
        //Simply Wrap your Code between this two conditions.
    }
}

As you can see here:

First you have to check whether the request is POST or not.

Second than you have to check whether it's valid for your form or not.

Indrasinh Bihola
  • 2,094
  • 3
  • 23
  • 25