Merge branch '3-login-with-username' into 'master'
Resolve "Login with username" Closes #3 See merge request pnx/httpcb!3
This commit is contained in:
commit
34f639e964
3 changed files with 24 additions and 9 deletions
|
|
@ -30,11 +30,6 @@ class Login extends Form
|
|||
'placeholder' => 'Username/Email',
|
||||
));
|
||||
|
||||
$validator = new EmailValidator(array(
|
||||
'message' => 'The e-mail is not valid',
|
||||
));
|
||||
$email->addValidator($validator);
|
||||
|
||||
$this->add($email);
|
||||
|
||||
// Password
|
||||
|
|
|
|||
|
|
@ -12,13 +12,19 @@ class Auth extends Component
|
|||
*/
|
||||
protected $_session_key = 'auth';
|
||||
|
||||
public function login($email, $password)
|
||||
/**
|
||||
* Login using email/user + password combination.
|
||||
*
|
||||
* @param string $email_username
|
||||
* @param string $password
|
||||
* @return bool
|
||||
*/
|
||||
public function login($email_username, $password)
|
||||
{
|
||||
// Look for a user with this email.
|
||||
$user = User::findFirstByEmail($email);
|
||||
// Look for a user with this username/email.
|
||||
$user = User::findFirstByUsernameOrEmail($email_username);
|
||||
|
||||
if ($user) {
|
||||
|
||||
// Verify password
|
||||
$hash = $user->getPassword();
|
||||
if (strlen($hash) > 1 && password_verify($password, $hash)) {
|
||||
|
|
|
|||
|
|
@ -126,4 +126,18 @@ class User extends Model
|
|||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the first user by Username or Email
|
||||
*
|
||||
* @param string $value
|
||||
* @return User|bool
|
||||
*/
|
||||
static public function findFirstByUsernameOrEmail($value)
|
||||
{
|
||||
return self::findFirst([
|
||||
"email = :v: OR username = :v:",
|
||||
"bind" => [ 'v' => $value ]
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue