add email signup flow
This commit is contained in:
parent
9945163a2f
commit
3dc9204979
27 changed files with 629 additions and 24 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue