add authenticate user use case

This commit is contained in:
Yisroel Baum 2026-04-24 13:21:02 +03:00
parent fd5278b3fe
commit 79d9ece2ae
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9

View file

@ -0,0 +1,48 @@
<?php
namespace App\User\UseCases;
use App\Exceptions\BadRequestException;
use App\Exceptions\UnauthorizedException;
use App\User\User;
use App\User\UserRepository;
use App\ValueObjects\EmailAddress;
class AuthenticateUser
{
public function __construct(
private UserRepository $userRepo,
) {}
/**
* @throws BadRequestException
* @throws UnauthorizedException
*/
public function execute(AuthenticateUserRequest $request): User
{
if ($request->email === null) {
throw new BadRequestException('email is required');
}
if ($request->password === null) {
throw new BadRequestException('password is required');
}
$user = $this->userRepo->findByEmail(
new EmailAddress($request->email)
);
if ($user === null) {
throw new UnauthorizedException('invalid credentials');
}
$passwordMatches = password_verify(
$request->password,
$user->getPasswordHash()
);
if (!$passwordMatches) {
throw new UnauthorizedException('invalid credentials');
}
return $user;
}
}