Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"autoload": {
"psr-4": {
"Authentication\\": "src/Authentication"
"Authentication\\": "src/Authentication",
"Infrastructure\\": "src/Infrastructure"
}
},
"require-dev": {
Expand Down
15 changes: 10 additions & 5 deletions public/login.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
<?php

$user = $_POST['emailAddress'];
require_once __DIR__ . '/../vendor/autoload.php';

$email = $_POST['emailAddress'];
$password = $_POST['password'];

$existingUsers = json_decode(file_get_contents(__DIR__ . '/../data/users.json'), true);
$users = new \Infrastructure\Authentication\Repository\FilesystemUsers(__DIR__ . '/../data/users.json');

if (!isset($existingUsers[$user])) {
if (! $users->exists($email)) {
echo 'Nope';

return;
}

if (! password_verify($password, $existingUsers[$user])) {
$user = $users->get($email);

if (! $user->authenticate($password)) {
echo 'Nope';

return;
}

echo 'Ok';
echo 'OK';

19 changes: 11 additions & 8 deletions public/register.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
<?php

// registering a new user:
require_once __DIR__ . '/../vendor/autoload.php';

$email = $_POST['emailAddress'];
$password = $_POST['password'];

$existingUsers = json_decode(file_get_contents(__DIR__ . '/../data/users.json'), true);
$users = new \Infrastructure\Authentication\Repository\FilesystemUsers(__DIR__ . '/../data/users.json');

if (isset($existingUsers[$email])) {
if ($users->exists($email)) {
echo 'Already registered';

return;
}

$hash = password_hash($password, \PASSWORD_DEFAULT);
$user = new \Authentication\Entity\User(
$email,
$password
);

$existingUsers[$email] = $hash;
$users->store($user);

error_log('Registration mail sent here');

file_put_contents(__DIR__ . '/../data/users.json', json_encode($existingUsers));
/** @var Notifier $notify */
// send email abstraction?
//error_log('Registration mail sent here');

echo 'OK';

17 changes: 16 additions & 1 deletion src/Authentication/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@

class User
{

/** @var string */
public $email;

/** @var string */
public $passwordHash;

public function __construct(string $email, string $password)
{
$this->email = $email;
$this->passwordHash = password_hash($password, \PASSWORD_DEFAULT);
}

public function authenticate(string $password) : bool
{
return password_verify($password, $this->passwordHash);
}
}
3 changes: 2 additions & 1 deletion src/Authentication/Repository/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

interface Users
{
public function exists(string $emailAddress) : bool;
public function get(string $emailAddress) : User;
public function store(User $user) : void;
}
}
55 changes: 55 additions & 0 deletions src/Infrastructure/Authentication/Repository/FilesystemUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Infrastructure\Authentication\Repository;

use Authentication\Entity\User;
use Authentication\Repository\Users;

final class FilesystemUsers implements Users
{
/** @var string */
private $usersPath;

public function __construct(string $usersPath)
{
$this->usersPath = $usersPath;
}

public function exists(string $emailAddress) : bool
{
return isset($this->existingUsers()[$emailAddress]);
}

public function get(string $emailAddress) : User
{
$existingUsers = $this->existingUsers();

if (! isset($existingUsers[$emailAddress])) {
throw new \UnexpectedValueException(sprintf('User %s does not exist', $emailAddress));
}

$user = (new \ReflectionClass(User::class))->newInstanceWithoutConstructor();

$user->email = $emailAddress;
$user->passwordHash = $existingUsers[$emailAddress];

return $user;
}

public function store(User $user) : void
{
$existingUsers = $this->existingUsers();

$existingUsers[$user->email] = $user->passwordHash;

file_put_contents($this->usersPath, json_encode($existingUsers));
}

/** @return array<string, string> */
private function existingUsers() : array
{
return json_decode(file_get_contents($this->usersPath), true);
}
}