Zend Create User and Automatically Sign them in

960 Views Asked by At

I have search and search but no answer I am using Zend Framework to create a user and on success store the record in zend auth so they are automatically logged in.

I have the create user working perfect but after the success I need to start the _getAuthAdpater() and sign the user in.

I have implemented the below code but it will not store the users data in the session. Here is my function.

public function registerAction()

$proUser = new Application_Model_DbTable_ProUser;
$userId = $proUser->addProUser($data['email'], $data['name'], $data['password']);if($userId){

                // add new row into proBasic
                $proBasic = new Application_Model_DbTable_proBasic;
                $proBasic->addProBasic($userId);

                // add new row into proService
                $proService = new Application_Model_DbTable_proService;
                $proService->addProService($userId);                

                // Create auth session for proUser
                $adapter = $this->_getAuthAdapter();
                $adapter->setIdentity($data['email']); 
                $adapter->setCredential($data['password']);

                $auth = Zend_Auth::getInstance();
                $result = $auth->authenticate($adapter);

                if ($result->isValid()) {

                    $user = $adapter->getResultRowObject(array(
                        'proId',
                        'proName',
                        'proEmail',                     
                    ));                     
                    $auth->getStorage()->write($user);
                    return true;

                }

                return $this->_redirect('/auth/probasic/');
            }

Here is the _getAuthAdapter() function:

    private function _getAuthAdapter()
{

    $authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
    $authAdapter->setTableName('proUser')
                ->setIdentityColumn('proEmail')
                ->setCredentialColumn('proPassword');       

    return $authAdapter;            
}

I am getting no errors for this code but it isnt store the users details in the Auth. I have found 100's of example of how to create a login/sign up system in Zend and none of them automatically login the user, so I am wondering if I am trying to do something different.

Cheers

J.

1

There are 1 best solutions below

0
On BEST ANSWER

You don't really need to authenticate them if you're creating the session immediately after registration. What happens if you replace all your code after $proService->addProService($userId) with just this:

$auth = Zend_Auth::getInstance();
$auth->getStorage()->write($data['email']);