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,
) {}
}

View file

@ -0,0 +1,79 @@
<?php
namespace App\User\UseCases\SignupUser;
use App\Email\EmailConfirmationToken\UseCases\CreateEmailConfirmationToken;
use App\Email\EmailConfirmationToken\UseCases\CreateEmailConfirmationTokenRequest;
use App\Email\Emailer;
use App\Email\EmailFactory;
use App\Exceptions\BadRequestException;
use App\Shared\ValueObject\EmailAddress;
use App\User\CreateUserDto;
use App\User\User;
use App\User\UserRepository;
use DomainException;
use InvalidArgumentException;
class SignupUser
{
public function __construct(
private UserRepository $userRepository,
private CreateEmailConfirmationToken $createToken,
private Emailer $emailer,
private EmailFactory $emailFactory,
) {}
/**
* @throws BadRequestException
* @throws DomainException
*/
public function execute(SignupUserRequest $request): void
{
if ($request->email === null || trim($request->email) === '') {
throw new BadRequestException('email is required');
}
try {
$email = new EmailAddress($request->email);
} catch (InvalidArgumentException) {
throw new BadRequestException('email must be valid');
}
$user = $this->findOrCreatePendingUser($email);
$token = $this->createToken->execute(
new CreateEmailConfirmationTokenRequest(
user: $user,
minuteOffset: 10,
),
);
$body = $this->emailFactory->makeConfirmationEmail(
$token->getToken(),
);
$this->emailer->send(
$user->getEmail(),
'Confirm your Attainly email',
$body,
);
}
/**
* @throws DomainException
*/
private function findOrCreatePendingUser(EmailAddress $email): User
{
$user = $this->userRepository->findByEmail($email);
if ($user === null) {
return $this->userRepository->create(new CreateUserDto(
email: $email,
passwordHash: null,
));
}
if ($user->getPasswordHash() !== null) {
throw new DomainException(
"{$email->value()} already has an account",
);
}
return $user;
}
}

View file

@ -0,0 +1,8 @@
<?php
namespace App\User\UseCases\SignupUser;
final readonly class SignupUserRequest
{
public function __construct(public ?string $email) {}
}