general_field('email', $values, $name, $label, $attrs);
}
+function password_field($values, $name, $label, $attrs=[]) {
+ general_field('password', $values, $name, $label, $attrs);
+}
+
function hidden_field($name, $value) {
general_bare_field('hidden', [$name=>$value], $name);
}
--- /dev/null
+<?php
+require_once('includes/utils.php');
+require_once('includes/html-templating.php');
+require_once('includes/fields.php');
+require_once('includes/form-validation.php');
+require_once('includes/database.php');
+
+function validate_login(&$errors, $data) {
+ require_fields($errors, $data, ['email_address', 'password']);
+ if (!empty($errors)) return false;
+
+ $record = fetch_record('users', 'id,password',
+ simple_where('email_address', $data['email_address']));
+
+ // Still compare against a dummy hash when user not found to try and mitigate
+ // against simple timing attacks for existance of users.
+ $hash = password_hash('TestPassword', PASSWORD_DEFAULT);
+ if (!empty($record)) $hash = $record['password'];
+
+ $verified = password_verify($data['password'], $hash);
+
+ if (empty($record) || !$verified) $errors[] = 'Invalid email/password.';
+
+ return empty($errors) ? $record['id'] : false;
+}
+
+function page_title() {
+ return 'Login';
+}
+
+function additional_stylesheets() {
+ stylesheet('fields');
+}
+
+function content() {
+ $params = $errors = [];
+ if (array_key_exists('login', $_POST)) {
+ $uid = validate_login($errors, $_POST);
+ if ($uid) {
+ esc('Login successful!.');
+ die();
+ }
+ $params['email_address'] = $_POST['email_address'];
+ }?>
+
+ <h1>Login</h1>
+
+ <?php show_error_list($errors);?>
+
+ <form method="post" action=""><?php
+ email_field($params, 'email_address', 'Email address', ['required'=>'']);
+ password_field($params, 'password', 'Password', ['required'=>'']);?>
+ <input type="submit" name="login" value="Login" />
+ </form><?php
+}
+
+require_once('includes/template.php');
+?>