kwv84
kwv84

Reputation: 953

zf2 How to get user details with AuthenticationService



I've created a module to authenticate a user. Now, after login I go to the index action and the system tells me that the authentication is all working fine. But What I want is to print some more user details from the Users table. When I try:

print_r($this->getServiceLocator()->get('AuthService')->getAdapter()->getResultRowObject());

I get no result. What am I doing wrong? Thanks for your help.

In my module.php I've the following code(snippet):

public function getServiceConfig()
{
    return array(
        'abstract_factories' => array(),
        'aliases' => array(),
        'factories' => array(

            // Some more code here but removed for simplicity
            // Autentication
            'AuthService' => function ($sm) {
                $adapter = $sm->get('master_db');
                $dbAuthAdapter = new DbAuthAdapter ( $adapter, 'Users', 'email', 'password' );

                $auth = new AuthenticationService();
                $auth->setAdapter ( $dbAuthAdapter );

                return $auth;
            },
            // Some more code here but removed for simplicity

}

In my IndexController.php I've the following (snippets):

public function indexAction()
{

    if(!$this->getServiceLocator()->get('AuthService')->hasIdentity()){      
        return $this->redirect()->toUrl('login');
    }
    echo "hello, it works!";         
    exit;

}

public function loginAction(){
    $form = $this->getServiceLocator()->get('LoginForm');
    $viewModel = new ViewModel(array('form' =>
    $form));

        return $viewModel;
}

public function processAction(){

    // Lots of code here
    if($bcrypt->verify($loginData['password'], $userData->password))
    {

        $this->getAuthService()
            ->getAdapter()
            ->setIdentity($loginData['email'])
            ->setCredential($userData->password);
            $result = $this->getAuthService()->authenticate();  
    }

    // Lots of code here where I check if $result->isValid and route to the
    // correct action

}

public function getAuthService() {
        if(!isset($this->authservice)) {

            $this->authservice = $this->getServiceLocator()->get('AuthService');
        }

        return $this->authservice;
}

Upvotes: 1

Views: 4899

Answers (1)

NiMeDia
NiMeDia

Reputation: 1004

Instead of refering to the authentication result object (which properly only exists in the authentication request) you can simply store user details in the authentication identity (@see http://framework.zend.com/manual/2.1/en/modules/zend.authentication.intro.html).

For your case you could also store user specific details right after the validation of the authentication result in the authentication storage:

if ($result->isValid()) { //authentication success $resultRow = $this->authService->getAdapter()->getResultRowObject(); $this->authService->getStorage()->write(array( 'id' => $resultRow->id, 'user_agent' => $request->getServer('HTTP_USER_AGENT')) ); }

(This information was taken from this authentication tutorial http://samsonasik.wordpress.com/2013/05/29/zend-framework-2-working-with-authenticationservice-and-db-session-save-handler/)

Upvotes: 3

Related Questions