Added login by Login or Email

This commit is contained in:
Dmitry Khomutov 2017-01-05 01:39:23 +07:00
parent 60267cc315
commit 6a0a7a0bf2
2 changed files with 40 additions and 5 deletions

View file

@ -51,7 +51,7 @@ class SessionController extends Controller
} else {
unset($_SESSION['login_token']);
$user = $this->userStore->getByEmail($this->getParam('email'));
$user = $this->userStore->getByEmailOrName($this->getParam('email'));
if ($user && password_verify($this->getParam('password', ''), $user->getHash())) {
session_regenerate_id(true);
@ -69,7 +69,7 @@ class SessionController extends Controller
$form->setMethod('POST');
$form->setAction(APP_URL.'session/login');
$email = new b8\Form\Element\Email('email');
$email = new b8\Form\Element\Text('email');
$email->setLabel(Lang::get('email_address'));
$email->setRequired(true);
$email->setContainerClass('form-group');

View file

@ -52,17 +52,23 @@ class UserStoreBase extends Store
}
/**
*
* Get a single User by Email.
* @return null|User
*
* @param string $value
*
* @throws HttpException
*
* @return User
*/
public function getByEmail($value, $useConnection = 'read')
public function getByEmail($value)
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM `user` WHERE `email` = :email LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepare($query);
$stmt = Database::getConnection()->prepare($query);
$stmt->bindValue(':email', $value);
if ($stmt->execute()) {
@ -74,6 +80,35 @@ class UserStoreBase extends Store
return null;
}
/**
*
* Get a single User by Email or Name.
*
* @param string $value
*
* @throws HttpException
*
* @return User
*/
public function getByEmailOrName($value)
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM `user` WHERE `email` = :value OR `name` = :value LIMIT 1';
$stmt = Database::getConnection()->prepare($query);
$stmt->bindValue(':value', $value);
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new User($data);
}
}
return null;
}
/**
* Get multiple User by Name.
* @return array