Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leepeuker committed Jun 30, 2022
1 parent a60a10e commit fca4317
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
18 changes: 13 additions & 5 deletions src/Application/User/Service/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

class Authentication
{
private const AUTHENTICATION_COOKIE_NAME = 'id';

private const MAX_EXPIRATION_AGE_IN_DAYS = 30;

public function __construct(private readonly Repository $repository)
{
}
Expand All @@ -19,22 +23,26 @@ public function deleteToken(string $token) : void

public function isUserAuthenticated() : bool
{
$token = filter_input(INPUT_COOKIE, 'id');
$token = filter_input(INPUT_COOKIE, self::AUTHENTICATION_COOKIE_NAME);

if (empty($token) === false && $this->isValidToken($token) === true) {
return true;
}

if (empty($token) === false) {
unset($_COOKIE['id']);
setcookie('id', '', -1);
unset($_COOKIE[self::AUTHENTICATION_COOKIE_NAME]);
setcookie(self::AUTHENTICATION_COOKIE_NAME, '', -1);
}

return false;
}

public function login(string $password, bool $rememberMe) : void
{
if ($this->isUserAuthenticated() === true) {
return;
}

$user = $this->repository->fetchAdminUser();

if (password_verify($password, $user->getPasswordHash()) === false) {
Expand All @@ -43,12 +51,12 @@ public function login(string $password, bool $rememberMe) : void

$expirationDate = $this->createExpirationDate();
if ($rememberMe === true) {
$expirationDate = $this->createExpirationDate(30);
$expirationDate = $this->createExpirationDate(self::MAX_EXPIRATION_AGE_IN_DAYS);
}

$token = $this->generateToken(DateTime::createFromString((string)$expirationDate));

setcookie('id', $token, (int)$expirationDate->format('U'));
setcookie(self::AUTHENTICATION_COOKIE_NAME, $token, (int)$expirationDate->format('U'));
}

private function createExpirationDate(int $days = 1) : DateTime
Expand Down
8 changes: 0 additions & 8 deletions src/HttpController/AuthenticationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ public function __construct(

public function login(Request $request) : Response
{
if ($this->authenticationService->isUserAuthenticated() === true) {
return Response::create(
StatusCode::createSeeOther(),
null,
[Header::createLocation($_SERVER['HTTP_REFERER'])]
);
}

try {
$this->authenticationService->login(
$request->getPostParameters()['password'],
Expand Down

0 comments on commit fca4317

Please sign in to comment.