Hello I’m new to Yii , I’m using Yii2 basic, I tried to create login with database but I did’t yet get it I get error : Incorrect username or password.
my model is :
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "user_login".
*
* @property integer $id
* @property string $username
* @property string $password
* @property string $authKey
* @property string $accessToken
* @property integer $USERS_ID
*
*/
class UserLogin extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
public $id;
public $username;
public $password;
public $authKey;
public $accessToken;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @inheritdoc
*/
public static function tableName()
{
return 'user_login';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'password', 'USERS_ID'], 'required'],
[['USERS_ID'], 'integer'],
[['username', 'password', 'authKey', 'accessToken'], 'string', 'max' => 25],
[['USERS_ID'], 'exist', 'skipOnError' => true, 'targetClass' => Users::className(), 'targetAttribute' => ['USERS_ID' => 'USERS_ID']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Name',
'password' => 'Password',
'authKey' => 'Auth Key',
'accessToken' => 'Access Token',
'USERS_ID' => 'Users ID',
];
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return self::findOne($id);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new yii\base\NotSupportedException();
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return self::findOne(['username'=>$username]);
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return bool if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->password === $password;
}
}
and my login form is
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = UserLogin::findByUsername($this->username);
}
return $this->_user;
}
}