api-login change creds

This commit is contained in:
Денис Хорош 2023-12-15 19:30:25 +03:00
parent 932ea915de
commit ecd32b6529
2 changed files with 16 additions and 5 deletions

View File

@ -120,6 +120,17 @@ class User extends ActiveRecord implements IdentityInterface, UserRbacInterface
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]); return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
} }
/**
* Finds user by email
*
* @param string $email
* @return static|null
*/
public static function findByEmail($email)
{
return static::findOne(['username' => $email, 'status' => self::STATUS_ACTIVE]);
}
/** /**
* Finds user by password reset token * Finds user by password reset token
* *

View File

@ -11,7 +11,7 @@ use yii\base\Model;
*/ */
class LoginForm extends Model class LoginForm extends Model
{ {
public $username; public $email;
public $password; public $password;
public $rememberMe = true; public $rememberMe = true;
@ -25,8 +25,8 @@ class LoginForm extends Model
public function rules() public function rules()
{ {
return [ return [
// username and password are both required // email and password are both required
[['username', 'password'], 'required'], [['email', 'password'], 'required'],
// rememberMe must be a boolean value // rememberMe must be a boolean value
['rememberMe', 'boolean'], ['rememberMe', 'boolean'],
// password is validated by validatePassword() // password is validated by validatePassword()
@ -39,7 +39,7 @@ class LoginForm extends Model
if (!$this->hasErrors()) { if (!$this->hasErrors()) {
$user = $this->getUser(); $user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) { if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.'); $this->addError($attribute, 'Incorrect e-mail or password.');
} }
} }
} }
@ -61,7 +61,7 @@ class LoginForm extends Model
public function getUser(): ?User public function getUser(): ?User
{ {
if ($this->_user === null) { if ($this->_user === null) {
$this->_user = User::findByUsername($this->username); $this->_user = User::findByEmail($this->email);
} }
return $this->_user; return $this->_user;