77363

after make or change password User dont login in Yii2

Question:

after make or edit password User i have login error 'Incorrect username or password' in Yii2

login with user 'admin' is work ( i make admin user with actionCreate1)

auth_key: kYm0pvYAXY4IzuV7eYgGgtjSqoxxMNUL password: $2y$13$QqsbMW3ErXwWOPad3abDYOPzh5XLwuEvQKBhZGEEDoT0Av5l0bE2S

but i make user or edit password , in login page i have error: 'Incorrect username or password'

<strong>i think problem from beforeSave in Model USER</strong>

User Table:

id int auth_key text username text password text

actionCreate: <strong>it's not work</strong>

public function actionCreate() { $model = new User(); if ($model->load(Yii::$app->request->post())) { if($model->save()) { $model->img = UploadedFile::getInstance($model, 'img'); if($model->img !== null) $model->upload('img',$model->id); $model->save(); } return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } }

actionCreate1: <strong>it's GOOD WORK</strong>

public function actionCreate() { $model = new User(); if ($model->load(Yii::$app->request->post())) { $model->username = Yii::$app->request->post('User')['username']; $model->password = Yii::$app->request->post('User')['password']; $model->save(); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', [ 'model' => $model, ]); } }

Model User:

public function beforeSave($insert) { if($this->password) { $this->setPassword($this->password); } if($insert) { $this->generateAuthKey(); } return parent::beforeSave($insert); } public function setPassword($password) { $this->password = Yii::$app->security->generatePasswordHash($password); } public function generateAuthKey() { $this->auth_key = Yii::$app->security->generateRandomString(); } /** * @param $password * @return bool */ public function validatePassword($password) { return Yii::$app->security->validatePassword($password, $this->password); } /** * @inheritdoc */ public static function findIdentity($id) { return static::findOne([ 'id' => $id ]); } public static function findIdentityByAccessToken($token, $type = null) {} /** * @param $username * @return null|static */ public static function findByUsername($username) { return static::findOne([ 'username' => $username, ]); } /** * @inheritdoc */ public function getId() { return $this->id; } /** * @inheritdoc */ public function getAuthKey() { return $this->auth_key; } /** * @inheritdoc */ public function validateAuthKey($authKey) { return $this->auth_key === $authKey; }

Answer1:

You are using the same name for attribute storing password hash and raw password.

Either change password hash attribute to something like passwod_hash or raw password attribute to something like raw_password and modify the code accordingly to handle it.

Answer2:

for fix this problem.

step1: change before save Model USER

public function beforeSave($insert) { if($insert) { if($this->password) { $this->setPassword($this->password); } $this->generateAuthKey(); } return parent::beforeSave($insert); }

step2: small change in update user

In the first code $password = $model->password; if(Yii::$app->request->post('User')['password'] != null) { $model->password = Yii::$app->security->generatePasswordHash(Yii::$app->request->post('User')['password']); } else { $model->password = $password; }

Recommend

  • Why i getting an error “Call to a member function formName() on a non-object”
  • Yii2: Call to a member function saveAs() on null while uploading multiple file
  • replace data from same id user in Yii framework
  • How to set a flash message in Yii2?
  • how to remove module name from url in yii [duplicate]
  • Yii2 database session - store additional attributes and user information
  • Yii: any way to save the images in compressed form?
  • Yii2: Finding file and getting path in a directory tree
  • pillow imaging ImportError
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Loading .coffee files via a view in Rails
  • Create DicomImage from scratch using Dcmtk
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • trying to dynamically update Highchart column chart but series undefined
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How to stop GridView from loading again when I press back button?
  • unknown Exception android
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?