I am working with zend 2 authentication. Now there is a case that a user can login with a user name and password or email and password. Is it possible to give both username and email as identity in zend 2. Otherwise how can i manage the situation?
This is my current working code. Here am using email as identity and password as credential.
in Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'AuthService' => function($sm) {
$dbTableAuthAdapter = new DbTableAuthAdapter(
$sm->get('Zend\Db\Adapter\Adapter'),
'users',
'email',
'password'
);
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
return $authService;
},
)
);
}
and in controller,
$this->getAuthService()->getAdapter()->setIdentity($oRequest->getPost('username'))->setCredential(md5($oRequest->getPost('password')));
$select = $this->getAuthService()->getAdapter()->getDbSelect();
$select->where('is_active = 1');
$oResult = $this->getAuthService()->authenticate();
// Authentication ends here
if ($oResult->isValid()) {
// code after authentication
}
Any ideas? Thank you.
Zend\Authentication\Adapter\DbTable\AbstractAdapter
. You could modify the 'identity column' to be an array of valid column names and use these when building the select query inauthenticateCreateSelect
. – AlexP