CakePHP 版本:3.3.5
我正在构建一个简单的系统,用户可以使用该系统登录(使用电子邮件和密码),登录后他们可以更改密码。
为此,我正在使用DefaultPasswordHasher
我的数据库中已经有几个用户。他们的记录已经存在。因此,当我执行登录功能时,它起作用了。我将用户输入的密码与数据库中已经存在的密码进行了比较。检查成功,用户可以登录。
现在登录后,我写了更改密码功能,更新了用户密码。新的哈希字符串替换了旧的密码字符串,但是当我再次尝试登录时,登录失败。
我将在这里分享我的控制器。这是非常基本的。
namespace Api\Controller;
use Cake\Utility\Security;
use Cake\Utility\Hash;
use Cake\Auth\DefaultPasswordHasher;
use Api\Controller\AppController;
class LoginController extends AppController
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
}
//Function to reset the password
public function resetPassword()
{
$pass = $this->request->data['pass'];
$hasher = new DefaultPasswordHasher();
$hashedPass = $hasher->hash($pass);
$this->loadModel('Login');
//save it to db
$responseArray = $this->Login->resetPassword($hashedPass);
$this->set(compact('responseArray'));
$this->set('_serialize', ['responseArray']);
}
//Function to login
public function login()
{
if ($this->request->is('post'))
{
//Password submitted via form
$pass = $this->request->data['pass'];
//Hashed password fetched from db via a function call
$actualPassword = 'hashedPasswordString'
//Compare password submitted and hash from db
if($this->checkPassword($pass,$actualPassword))
{
$result = 'password matched';
}
else
{
$result = 'password doesnot match';
}
}
$this->set(compact('result'));
$this->set('_serialize', ['result']);
}
//Function to compare password and hash
public function checkPassword($passedPassword , $actualPassword)
{
if ((new DefaultPasswordHasher)->check($passedPassword, $actualPassword)) {
return true;
} else {
return false;
}
}
}
谁能告诉我为什么密码不匹配。我是 CakePHP 框架的新手。提前致谢!