add email signup flow
This commit is contained in:
parent
9945163a2f
commit
3dc9204979
27 changed files with 629 additions and 24 deletions
|
|
@ -8,6 +8,6 @@ final readonly class CreateUserDto
|
|||
{
|
||||
public function __construct(
|
||||
public EmailAddress $email,
|
||||
public string $passwordHash,
|
||||
public ?string $passwordHash,
|
||||
) {}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\User;
|
||||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use DomainException;
|
||||
|
||||
class EloquentUserRepository implements UserRepository
|
||||
{
|
||||
|
|
@ -38,6 +39,22 @@ class EloquentUserRepository implements UserRepository
|
|||
return $this->toDomain($model);
|
||||
}
|
||||
|
||||
public function update(User $user): User
|
||||
{
|
||||
$model = UserModel::find($user->getId());
|
||||
if ($model === null) {
|
||||
throw new DomainException(
|
||||
"User with id {$user->getId()} not found",
|
||||
);
|
||||
}
|
||||
|
||||
$model->email = $user->getEmail()->value();
|
||||
$model->passwordHash = $user->getPasswordHash();
|
||||
$model->save();
|
||||
|
||||
return $this->toDomain($model);
|
||||
}
|
||||
|
||||
private function toDomain(UserModel $model): User
|
||||
{
|
||||
return new User(
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace App\User\UseCases\ConfirmUserEmail;
|
||||
|
||||
final readonly class ConfirmUserEmailRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $token,
|
||||
public ?string $password,
|
||||
) {}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace App\User\UseCases\SignupUser;
|
||||
|
||||
final readonly class SignupUserRequest
|
||||
{
|
||||
public function __construct(public ?string $email) {}
|
||||
}
|
||||
|
|
@ -4,12 +4,12 @@ namespace App\User;
|
|||
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
|
||||
final readonly class User
|
||||
final class User
|
||||
{
|
||||
public function __construct(
|
||||
private int $id,
|
||||
private EmailAddress $email,
|
||||
private string $passwordHash,
|
||||
private ?string $passwordHash,
|
||||
) {}
|
||||
|
||||
public function getId(): int
|
||||
|
|
@ -22,8 +22,13 @@ final readonly class User
|
|||
return $this->email;
|
||||
}
|
||||
|
||||
public function getPasswordHash(): string
|
||||
public function getPasswordHash(): ?string
|
||||
{
|
||||
return $this->passwordHash;
|
||||
}
|
||||
|
||||
public function setPasswordHash(string $passwordHash): void
|
||||
{
|
||||
$this->passwordHash = $passwordHash;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Model;
|
|||
/**
|
||||
* @property int $id
|
||||
* @property string $email
|
||||
* @property string $passwordHash
|
||||
* @property string|null $passwordHash
|
||||
*
|
||||
* @method static Builder<static>|UserModel newModelQuery()
|
||||
* @method static Builder<static>|UserModel newQuery()
|
||||
|
|
|
|||
|
|
@ -11,4 +11,6 @@ interface UserRepository
|
|||
public function find(int $id): ?User;
|
||||
|
||||
public function findByEmail(EmailAddress $email): ?User;
|
||||
|
||||
public function update(User $user): User;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue