Hashing a password in CakePHP3

Hashing a password in CakePHP3

Sometimes you need to hash a password.  For example, you may wish to determine if the user's password matches their current password.  In this example, we show you how to automatically hash a password when saving the user information, so this goes a bit further than simple hashing.

In your \src\Model\Entity\User.php, add the following:

use Cake\Auth\DefaultPasswordHasher;

Then, add the following function:

protected function _setPassword($password) {
        if (strlen($password) > 0) {
            return (new DefaultPasswordHasher)->hash($password);
        }
}     

Share this Post