authentication - Yii2 Login from DB (Setting unknown property: app\models\User::password_hash) -
i want user authentication in yii based on user table in database. user model:
<?php namespace app\models; use yii; use yii\base\notsupportedexception; use yii\db\activerecord; use yii\helpers\security; use yii\web\identityinterface; /** * model class table "user". * * @property integer $id * @property string $username * @property string $password * @property string $title */ class user extends \yii\db\activerecord implements identityinterface { public static function tablename() { return 'user'; } /** * @inheritdoc */ public function rules() { return [ [['username', 'password'], 'required'], [['username', 'password'], 'string', 'max' => 100] ]; } /** * @inheritdoc */ public function attributelabels() { return [ 'id' => 'userid', 'username' => 'username', 'password' => 'password', ]; } public static function findidentity($id) { return static::findone($id); } public static function findidentitybyaccesstoken($token, $type = null) { return static::findone(['access_token' => $token]); } public static function findbyusername ($username){ return static::findone(['username' => $username]); } public static function findbypasswordresettoken($token) { $expire= \yii::$app->params['user.passwordresettokenexpire']; $parts = explode('_', $token); $timestamp = (int) end($parts); if ($timestamp + $expire < time()) { //token expired return null; } return static::findone(['password_reset_token' => $token ]); } public function getid() { return $this->getprimarykey(); } public function getauthkey() { return $this->auth_key; } public function validateauthkey($authkey) { return $this->getauthkey() === $authkey; } public function validatepassword($password){ $this->password_hash= yii::$app->security->generatepasswordhash ($password); } public function generateauthkey() { $this->auth_key = yii::$app->security->generaterandomkey(); } /** * generates new password reset token */ public function generatepasswordresettoken() { $this->password_reset_token = yii::$app->security->generaterandomkey() . '_' . time(); } /** * removes password reset token */ public function removepasswordresettoken() { $this->password_reset_token = null; } }
but giving me error when try login:
setting unknown property: app\models\user::password_hash
this actionlogin in sitecontroller:
public function actionlogin() { if (!\yii::$app->user->isguest) { return $this->gohome(); } $model = new loginform(); if ($model->load(yii::$app->request->post()) && $model->login()) { return $this->goback(); } else { return $this->render('login', [ 'model' => $model, ]); } }
and code in loginform.php:
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 user using provided username , password. * @return boolean whether user logged in */ public function login() { if ($this->validate()) { return yii::$app->user->login($this->getuser(), $this->rememberme ? 3600*24*30 : 0); } else { return false; } }
i don't know wrong, please me fix this?
this because column "password_hash" assigned in function "validatepassword()" doesn't exist in database or not declared in user model.
if password hash stored in database in "password" field, change "validatepassword()" to,
public function validatepassword($password) { return $this->password === yii::$app->security->generatepasswordhash ($password); }
Comments
Post a Comment