# CakePHP 5 custom authentication, PasswordHasher

**URL:** <https://discourse.cakephp.org/t/cakephp-5-custom-authentication-passwordhasher/12150>\
**Category:** Need Help\
**Created:** [July 28, 2024, 3:36pm UTC](https://discourse.cakephp.org/t/cakephp-5-custom-authentication-passwordhasher/12150 "2024-07-28T15:36:17Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Salines](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/salines/32/3420_2.png) [@Salines](https://discourse.cakephp.org/u/Salines)\
**Post date:** [July 29, 2024, 6:04pm UTC](https://discourse.cakephp.org/t/cakephp-5-custom-authentication-passwordhasher/12150/6 "2024-07-29T18:04:46Z")

</div>

I would try something like this:

**create:** src/Authentication/PasswordHasher/PhpassPasswordHasher.php

```auto
<?php
namespace App\Authentication\PasswordHasher;

use Authentication\PasswordHasher\AbstractPasswordHasher;

class PhpassPasswordHasher extends AbstractPasswordHasher
{
    public function hash($password)
    {
        // your code
        return 'xyz';
    }

    public function check($password, $hashedPassword)
    {
        // your code
        return true;
    }
}

```

**@ User Entity:**

```auto
namespace App\Model\Entity;

use Cake\ORM\Entity;
use App\Authentication\PasswordHasher\PhpassPasswordHasher;

class User extends Entity
{
    protected function _setUserPass(string $password) : ?string
    {
        if (strlen($password) > 0) {
            return (new PhpassPasswordHasher())->hash($password);
        }
        return null;
    }
}

```

**@ src/Application.php**

```auto
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
    $service = new AuthenticationService();

    // Load identifiers
    $service->loadIdentifier('Authentication.Password', [
        'fields' => [
            'username' => 'user_login',
            'password' => 'user_pass'
        ]
    ]);

    // Load the authenticators
    $service->loadAuthenticator('Authentication.Session');
    $service->loadAuthenticator('Authentication.Form', [
        'fields' => [
            'username' => 'user_login',
            'password' => 'user_pass'
        ],
        'loginUrl' => '/users/login'
    ]);

    // Using the custom password hasher
    $service->setConfig([
        'passwordHasher' => [
            'className' => PhpassPasswordHasher::class
        ]
    ]);

    return $service;
}

```

PS, I haven’t tested the code.

---

_[View the full topic](https://discourse.cakephp.org/t/cakephp-5-custom-authentication-passwordhasher/12150)._
