add email signup flow

This commit is contained in:
Yisroel Baum 2026-08-03 20:26:08 +03:00
parent 9945163a2f
commit 3dc9204979
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
27 changed files with 629 additions and 24 deletions

View file

@ -0,0 +1,61 @@
<?php
namespace App\User\UseCases\ConfirmUserEmail;
use App\Auth\Clock;
use App\Auth\PasswordHasher;
use App\Email\EmailConfirmationToken\EmailConfirmationTokenRepository;
use App\Exceptions\BadRequestException;
use App\User\User;
use App\User\UserRepository;
use DomainException;
class ConfirmUserEmail
{
public function __construct(
private EmailConfirmationTokenRepository $tokenRepository,
private UserRepository $userRepository,
private PasswordHasher $passwordHasher,
private Clock $clock,
) {}
/**
* @throws BadRequestException
* @throws DomainException
*/
public function execute(ConfirmUserEmailRequest $request): User
{
if ($request->token === null || $request->token === '') {
throw new BadRequestException('token is required');
}
if ($request->password === null || $request->password === '') {
throw new BadRequestException('password is required');
}
if (strlen($request->password) < 8) {
throw new BadRequestException(
'password must be at least 8 characters',
);
}
$token = $this->tokenRepository->findByToken($request->token);
if ($token === null) {
throw new DomainException('token not found');
}
if ($token->getAvailableTo() < $this->clock->now()) {
throw new DomainException('token expired');
}
$user = $token->getUser();
if ($user->getPasswordHash() !== null) {
throw new DomainException('account already confirmed');
}
$user->setPasswordHash(
$this->passwordHasher->hash($request->password),
);
$confirmedUser = $this->userRepository->update($user);
$this->tokenRepository->delete($token->getId());
return $confirmedUser;
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace App\User\UseCases\ConfirmUserEmail;
final readonly class ConfirmUserEmailRequest
{
public function __construct(
public ?string $token,
public ?string $password,
) {}
}