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
27 changes: 18 additions & 9 deletions public/login.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
<?php

// 1. fetch user by email
// 2. compare user password hash against given password
// 3. is the user banned? (optional)
// 4. log login (optional)
// 5. store user identifier into the session

// discuss: should the fetching by password happen at database level?
// Should it happen inside the entity?
// Or in a service?
$user = $_POST['emailAddress'];
$password = $_POST['password'];

$existingUsers = json_decode(file_get_contents(__DIR__ . '/../data/users.json'), true);
Copy link
Member Author

@Ocramius Ocramius Oct 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The storage is a simple file containing data in a format like:

{
   "[email protected]": "p@$$w0rdHa$h",
   "[email protected]": "p@$$w0rdHa$h"
}


if (!isset($existingUsers[$user])) {
echo 'Nope';

return;
}

if (! password_verify($password, $existingUsers[$user])) {
echo 'Nope';

return;
}

echo 'Ok';
26 changes: 20 additions & 6 deletions public/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

// registering a new user:

// 1. check if a user with the same email address exists
// 2. if not, create a user
// 3. hash the password
// 4. send the email to confirm activation (we will just display it)
// 5. save the user
$email = $_POST['emailAddress'];
$password = $_POST['password'];

$existingUsers = json_decode(file_get_contents(__DIR__ . '/../data/users.json'), true);

if (isset($existingUsers[$email])) {
echo 'Already registered';

return;
}

$hash = password_hash($password, \PASSWORD_DEFAULT);

$existingUsers[$email] = $hash;

error_log('Registration mail sent here');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error_log() is a sufficient replacement for simulating I/O here, since we're writing to the terminal output.


file_put_contents(__DIR__ . '/../data/users.json', json_encode($existingUsers));

echo 'OK';

// Tip: discuss - email or saving? Chicken-egg problem