copy user entity and auth from ysv
This commit is contained in:
parent
9d5bfc33a6
commit
613180d459
24 changed files with 612 additions and 0 deletions
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases\AuthenticateUser;
|
||||
|
||||
use App\Auth\PasswordHasher;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\UnauthorizedException;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\User;
|
||||
use App\User\UserRepository;
|
||||
|
||||
class AuthenticateUser
|
||||
{
|
||||
public function __construct(
|
||||
private UserRepository $userRepo,
|
||||
private PasswordHasher $hasher,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws UnauthorizedException
|
||||
*/
|
||||
public function execute(AuthenticateUserRequest $request): User
|
||||
{
|
||||
if ($request->email === null || $request->email === '') {
|
||||
throw new BadRequestException('email is required');
|
||||
}
|
||||
if ($request->password === null || $request->password === '') {
|
||||
throw new BadRequestException('password is required');
|
||||
}
|
||||
|
||||
$user = $this->userRepo->findByEmail(
|
||||
new EmailAddress($request->email)
|
||||
);
|
||||
if ($user === null) {
|
||||
throw new UnauthorizedException('invalid credentials');
|
||||
}
|
||||
|
||||
$passwordMatches = $this->hasher->verify(
|
||||
$request->password,
|
||||
$user->getPasswordHash(),
|
||||
);
|
||||
if (! $passwordMatches) {
|
||||
throw new UnauthorizedException('invalid credentials');
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases\AuthenticateUser;
|
||||
|
||||
class AuthenticateUserRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $email,
|
||||
public ?string $password,
|
||||
) {}
|
||||
}
|
||||
34
backend/app/Auth/UseCases/CreateSession/CreateSession.php
Normal file
34
backend/app/Auth/UseCases/CreateSession/CreateSession.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases\CreateSession;
|
||||
|
||||
use App\Auth\Clock;
|
||||
use App\Auth\CreateSessionDto;
|
||||
use App\Auth\Session;
|
||||
use App\Auth\SessionRepository;
|
||||
use App\Auth\TokenGenerator;
|
||||
use App\User\User;
|
||||
|
||||
class CreateSession
|
||||
{
|
||||
private const SESSION_LIFETIME = '+7 days';
|
||||
|
||||
public function __construct(
|
||||
private SessionRepository $sessionRepo,
|
||||
private TokenGenerator $tokenGenerator,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
public function execute(User $user): Session
|
||||
{
|
||||
$now = $this->clock->now();
|
||||
$expiresAt = $now->modify(self::SESSION_LIFETIME);
|
||||
|
||||
return $this->sessionRepo->create(new CreateSessionDto(
|
||||
token: $this->tokenGenerator->generate(),
|
||||
user: $user,
|
||||
createdAt: $now,
|
||||
expiresAt: $expiresAt,
|
||||
));
|
||||
}
|
||||
}
|
||||
17
backend/app/Auth/UseCases/Logout/Logout.php
Normal file
17
backend/app/Auth/UseCases/Logout/Logout.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases\Logout;
|
||||
|
||||
use App\Auth\SessionRepository;
|
||||
|
||||
class Logout
|
||||
{
|
||||
public function __construct(
|
||||
private SessionRepository $sessionRepo,
|
||||
) {}
|
||||
|
||||
public function execute(string $token): void
|
||||
{
|
||||
$this->sessionRepo->deleteByToken($token);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue