diff --git a/src/sql/initial-data.sql b/src/sql/initial-data.sql index ff3b101946b889826a54311f7a94630c4f0a998a..1d10beef2ea6b27cdec03ad9641e81f73270eadb 100644 --- a/src/sql/initial-data.sql +++ b/src/sql/initial-data.sql @@ -2,7 +2,9 @@ create table if not exists account ( account_id serial primary key, username varchar(30) unique, password varchar(60), - is_admin smallint + is_admin smallint, + timestamp timestamp default now(), + email varchar(60) ); create table if not exists token ( diff --git a/src/web/account/account-register.php b/src/web/account/account-register.php index 0f79d4212dadeb7ba6f201a42519e38026ba655b..2a66ed48db4f22c89502efe06141f0fd6729704e 100644 --- a/src/web/account/account-register.php +++ b/src/web/account/account-register.php @@ -5,13 +5,14 @@ require_once 'auth/token.php'; require_once 'db/db-executor.php'; require_once 'crypto/password.php'; -function registerAccount(string $username, string $password, bool $admin): string { - $query = "INSERT INTO account (username, password, is_admin) VALUES(:username, :password, :isAdmin) - RETURNING account_id"; +function registerAccount(string $username, string $password, bool $admin, string $email): string { + $query = "INSERT INTO account (username, password, is_admin, email) + VALUES(:username, :password, :isAdmin, :email) RETURNING account_id"; $params = [ 'username' => $username, 'password' => hashPassword($password), - 'isAdmin' => (int) $admin + 'isAdmin' => (int) $admin, + 'email' => $email ]; $res = execSelect($query, $params); diff --git a/src/web/presentation/account/css/register.css b/src/web/presentation/account/css/register.css index faed420b8ecd615525b6020687ace07d2496f727..cfda46dc9ef9d24d69bc9729994b365784691f51 100644 --- a/src/web/presentation/account/css/register.css +++ b/src/web/presentation/account/css/register.css @@ -71,6 +71,11 @@ body { border-bottom-right-radius: 0 !important; } +#input-mid { + border-top: none; + border-radius: 0; +} + #input-bot { border-top-left-radius: 0 !important; border-top-right-radius: 0 !important; diff --git a/src/web/presentation/account/js/register.js b/src/web/presentation/account/js/register.js index 6adda34590a884754c2a9049971b075e189a4690..a432063e47c31d2ee3d904619015213663eb09fd 100644 --- a/src/web/presentation/account/js/register.js +++ b/src/web/presentation/account/js/register.js @@ -1,9 +1,16 @@ +const isEmailValid = (email) => { + return email.match( + /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + ); +}; + function validate() { let username = document.forms["form"]["username"].value; + let email = document.forms["form"]["email"].value; let password = document.forms["form"]["password"].value; let valid = true, errMsg = ''; - if (username === "" || password === "") { + if (username === "" || password === "" || email === "") { errMsg = "Both username and password are required fields."; valid = false; } @@ -13,6 +20,11 @@ function validate() { valid = false; } + if (valid && !isEmailValid(email)) { + errMsg = "Email format is invalid."; + valid = false; + } + if (valid && password.length > 30) { errMsg = "Password must be less than 30 characters."; valid = false; diff --git a/src/web/presentation/account/register.php b/src/web/presentation/account/register.php index 74614939544073a6a4a584a13c20e5560cd8b5e5..8fb2991cba7c8fafb6face4dc7d7af054f56b59b 100644 --- a/src/web/presentation/account/register.php +++ b/src/web/presentation/account/register.php @@ -14,7 +14,8 @@ if (isset($_COOKIE['token'])) { if ($_SERVER["REQUEST_METHOD"] == "GET") { require 'presentation/account/register.view.php'; } else if ($_SERVER["REQUEST_METHOD"] == "POST") { - if (!isset($_POST["username"]) || !isset($_POST["password"])) { + if (!isset($_POST["username"]) || !isset($_POST["password"]) + || !isset($_POST["email"])) { header('Location: /403', true, 303); exit(); } @@ -33,8 +34,9 @@ if ($_SERVER["REQUEST_METHOD"] == "GET") { $username = $_POST['username']; $password = $_POST['password']; + $email = $_POST['email']; $isAdmin = isset($_POST['admin']) && $_POST['admin'] == 'on'; - $token = registerAccount($username, $password, $isAdmin); + $token = registerAccount($username, $password, $isAdmin, $email); if (!$token) { header('Location: /500', true, 303); diff --git a/src/web/presentation/account/register.view.php b/src/web/presentation/account/register.view.php index 4a05c39a0a3e189689359412cc276af2d28e6432..74045364371670e3a94a4405898c9e8d2b6ed1e3 100644 --- a/src/web/presentation/account/register.view.php +++ b/src/web/presentation/account/register.view.php @@ -24,6 +24,7 @@ <div id="form-d"> <form id="form" method="POST" onsubmit="return validate()"> <input type="text" name="username" class="input" id="input-top" placeholder="Username"> + <input type="email" name="email" class="input" id="input-mid" placeholder="Email"> <input type="password" name="password" class="input" id="input-bot" placeholder="Password"> <div id="cb-d"> <label for="input-cb" id="cb-label">Be a publisher?</label>