S
S
Slay102017-02-26 22:26:28
Yii
Slay10, 2017-02-26 22:26:28

Authentication problem with validatePassword()?

I can’t understand what’s wrong validatePassword() always raises an error regardless of whether the password is entered correctly or not, I do this in the Users model, I generate a password and save it to the database using the method

public function setPassword($password){
        $this->password = Yii::$app->getSecurity()->generatePasswordHash($password);
    }

the password is created normally and written to the database without any problems, now the validation method itself is in the model
public function validatePassword($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $user = $this->getUser();
            if (!$user || !Yii::$app->security->validatePassword($this->password, $user->password)) {
                var_dump($this->password . "------------" . Yii::$app->getSecurity()->validatePassword($this->password, $user->password) . "**********" . $user->password);
                $this->addError($attribute, "Пароль или имя пользователя введены неверно");
            }
        }

    }

var_dump outputs the following Slay--------------------**********$2y$13$Gnd.7dQCOpajawjRmn0laupNfj8tWAISLfYKazwmYHR
i.e. as I understand the password from the form and the hashed password from the database the function gets the correct only it doesn't return anything instead of emptiness between ---*** should be 0, and if you use a simple password and save it in the database and then compare there are no problems, everything works, maybe I'm missing something or forgot to connect it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Doniy, 2017-02-27
@doniys_a

var_dump($this->password . "------------" . Yii::$app->getSecurity()->validatePassword($this->password, $user->password) . "**********" . $user->password);

No 0 can be returned. F-I validatePassword returns either true or false. It does not save anything, does not update anything - it just checks the password that came in and set
. Because you are concatenating the string, nothing is displayed.
Output like this
var_dump($this->password,Yii::$app->getSecurity()->validatePassword($this->password, $user->password), $user->password);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question