inject PasswordHasher into CreateUser and AuthenticateUser

Replace direct password_hash and password_verify calls with the
injected PasswordHasher so the bcrypt cost can be substituted out
in tests. Production wiring is handled by the container's autowiring
of BcryptPasswordHasher.

This commit alone breaks the test suite because the existing tests
construct these use cases without the new dependency; the next
commit restores green by introducing FakePasswordHasher.
This commit is contained in:
Yisroel Baum 2026-04-26 09:06:21 +03:00
parent b1247d2fa1
commit 632085f5b6
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 6 additions and 2 deletions

View file

@ -2,6 +2,7 @@
namespace App\User\UseCases;
use App\Auth\PasswordHasher;
use App\Exceptions\BadRequestException;
use App\Exceptions\UnauthorizedException;
use App\User\User;
@ -12,6 +13,7 @@ class AuthenticateUser
{
public function __construct(
private UserRepository $userRepo,
private PasswordHasher $passwordHasher,
) {}
/**
@ -35,7 +37,7 @@ class AuthenticateUser
throw new UnauthorizedException('invalid credentials');
}
$passwordMatches = password_verify(
$passwordMatches = $this->passwordHasher->verify(
$request->password,
$user->getPasswordHash()
);

View file

@ -2,6 +2,7 @@
namespace App\User\UseCases;
use App\Auth\PasswordHasher;
use App\Exceptions\BadRequestException;
use App\User\User;
use App\User\UserRepository;
@ -11,6 +12,7 @@ class CreateUser
{
public function __construct(
private UserRepository $userRepo,
private PasswordHasher $passwordHasher,
) {}
/**
@ -39,7 +41,7 @@ class CreateUser
return $this->userRepo->create(new CreateUserDto(
email: $email,
passwordHash: password_hash($dto->password, PASSWORD_DEFAULT),
passwordHash: $this->passwordHasher->hash($dto->password),
isAdmin: $dto->isAdmin,
));
}