add email signup flow
This commit is contained in:
parent
9945163a2f
commit
3dc9204979
27 changed files with 629 additions and 24 deletions
79
backend/app/User/UseCases/SignupUser/SignupUser.php
Normal file
79
backend/app/User/UseCases/SignupUser/SignupUser.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue