align backend auth patterns
This commit is contained in:
parent
b3266b38c8
commit
299efe0839
23 changed files with 249 additions and 142 deletions
|
|
@ -2,68 +2,63 @@
|
|||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Auth\Clock;
|
||||
use App\Auth\CreateSessionDto;
|
||||
use App\Auth\SessionRepository;
|
||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUser;
|
||||
use App\Auth\UseCases\AuthenticateUser\AuthenticateUserRequest;
|
||||
use App\Auth\UseCases\CreateSession\CreateSession;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\UnauthorizedException;
|
||||
use App\Http\Middleware\AuthMiddleware;
|
||||
use App\Http\Requests\LoginRequest;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\Shared\Http\RequestInput;
|
||||
use App\User\User;
|
||||
use App\User\UserRepository;
|
||||
use DateInterval;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Cookie;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function login(
|
||||
LoginRequest $request,
|
||||
UserRepository $userRepository,
|
||||
SessionRepository $sessionRepository,
|
||||
Clock $clock,
|
||||
): JsonResponse
|
||||
public function __construct(
|
||||
private AuthenticateUser $authenticateUser,
|
||||
private CreateSession $createSession,
|
||||
) {}
|
||||
|
||||
public function login(Request $request): JsonResponse
|
||||
{
|
||||
/** @var array{email: string, password: string} $credentials */
|
||||
$credentials = $request->validated();
|
||||
$user = $userRepository->findByCredentials(
|
||||
new EmailAddress($credentials['email']),
|
||||
$credentials['password'],
|
||||
);
|
||||
if ($user === null) {
|
||||
$input = new RequestInput($request);
|
||||
|
||||
try {
|
||||
$user = $this->authenticateUser->execute(
|
||||
new AuthenticateUserRequest(
|
||||
email: $input->string('email'),
|
||||
password: $input->string('password'),
|
||||
)
|
||||
);
|
||||
} catch (BadRequestException $exception) {
|
||||
return new JsonResponse(
|
||||
['error' => 'invalid_credentials'],
|
||||
401,
|
||||
['error' => $exception->getMessage()], 400
|
||||
);
|
||||
} catch (UnauthorizedException $exception) {
|
||||
return new JsonResponse(
|
||||
['error' => $exception->getMessage()], 401
|
||||
);
|
||||
}
|
||||
|
||||
$sessionLifetime = (int) config('session.lifetime', 120);
|
||||
$createdAt = $clock->now();
|
||||
$expiresAt = $createdAt->add(
|
||||
new DateInterval("PT{$sessionLifetime}M"),
|
||||
);
|
||||
$session = $sessionRepository->create(new CreateSessionDto(
|
||||
token: bin2hex(random_bytes(32)),
|
||||
user: $user,
|
||||
createdAt: $createdAt,
|
||||
expiresAt: $expiresAt,
|
||||
));
|
||||
$session = $this->createSession->execute($user);
|
||||
|
||||
$response = new JsonResponse([
|
||||
'user' => $this->userPayload($user),
|
||||
]);
|
||||
$response->headers->setCookie(new Cookie(
|
||||
], 200);
|
||||
|
||||
return $response->withCookie(Cookie::create(
|
||||
name: AuthMiddleware::COOKIE_NAME,
|
||||
value: $session->getToken(),
|
||||
expire: $session->getExpiresAt(),
|
||||
path: $this->cookiePath(),
|
||||
domain: $this->cookieDomain(),
|
||||
secure: (bool) config('session.secure', false),
|
||||
expire: $session->getExpiresAt()->getTimestamp(),
|
||||
path: '/',
|
||||
domain: null,
|
||||
secure: false,
|
||||
httpOnly: true,
|
||||
sameSite: $this->cookieSameSite(),
|
||||
raw: false,
|
||||
sameSite: Cookie::SAMESITE_LAX,
|
||||
));
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function me(Request $request): JsonResponse
|
||||
|
|
@ -86,34 +81,4 @@ class AuthController extends Controller
|
|||
'email' => $user->getEmail()->value(),
|
||||
];
|
||||
}
|
||||
|
||||
private function cookiePath(): string
|
||||
{
|
||||
$path = config('session.path', '/');
|
||||
|
||||
return is_string($path) ? $path : '/';
|
||||
}
|
||||
|
||||
private function cookieDomain(): ?string
|
||||
{
|
||||
$domain = config('session.domain');
|
||||
|
||||
return is_string($domain) ? $domain : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ''|'lax'|'none'|'strict'|null
|
||||
*/
|
||||
private function cookieSameSite(): ?string
|
||||
{
|
||||
$sameSite = config('session.same_site', 'lax');
|
||||
|
||||
return match ($sameSite) {
|
||||
'' => '',
|
||||
Cookie::SAMESITE_LAX => Cookie::SAMESITE_LAX,
|
||||
Cookie::SAMESITE_NONE => Cookie::SAMESITE_NONE,
|
||||
Cookie::SAMESITE_STRICT => Cookie::SAMESITE_STRICT,
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,40 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array<int, string>>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'email' => [
|
||||
'required',
|
||||
'string',
|
||||
'email',
|
||||
'max:255',
|
||||
],
|
||||
'password' => [
|
||||
'required',
|
||||
'string',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
protected function prepareForValidation(): void
|
||||
{
|
||||
$email = $this->input('email');
|
||||
if (is_string($email)) {
|
||||
$this->merge(['email' => trim($email)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue