summaryrefslogtreecommitdiff
path: root/signin.php
diff options
context:
space:
mode:
Diffstat (limited to 'signin.php')
-rwxr-xr-xsignin.php61
1 files changed, 19 insertions, 42 deletions
diff --git a/signin.php b/signin.php
index c2ee5a6..4f76d13 100755
--- a/signin.php
+++ b/signin.php
@@ -16,52 +16,29 @@
<input type="submit" name="submit">
</form>
<?php
-include_once 'includes/error.php';
-
-function validate($data): string
-{
- $data = trim($data);
- $data = stripslashes($data);
- return htmlspecialchars($data);
-}
+include_once './includes/form/SignInForm.php';
+include_once './includes/error.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $errors = array();
-
- if (empty($_POST['user_name'])) {
- $errors[] = 'Please provide a username.';
- } else {
- $user_name = validate($_POST['user_name']);
- }
-
- if (empty($_POST['user_pass'])) {
- $errors[] = 'Please provide a password.';
- } else {
- $user_pass = $_POST['user_pass'];
- }
+ $form = new SignInForm();
+ $username = $form->validate_username($_POST['user_name']);
+ $password = $form->validate_password($_POST['user_pass']);
- if (!empty($errors)) {
- $errstr = 'Please check the following problems: <ul>';
- foreach ($errors as $err) {
- $errstr .= '<li>' . $err . '</li>';
- }
- $errstr .= '</ul>';
- trigger_error($errstr);
- } else {
- $user = new User();
- $user->get_by_name($user_name);
+ $form->on_success(function () use ($username, $password) {
+ $user = new User();
+ $user->get_by_name($username);
- if (!$user->has_value()) {
- trigger_error('There is no user with that name. Did you mean to <a href="register.php">create a new account?</a>');
- } else {
- if (!password_verify($user_pass, $user->password)) {
- echo 'Password does not match!';
- } else {
- Session::get()->sign_in($user);
- header("Location: index.php");
- }
- }
- }
+ if (!$user->has_value()) {
+ trigger_error('There is no user with that name. Did you mean to <a href="register.php">create a new account?</a>');
+ } else {
+ if (!password_verify($password, $user->password)) {
+ echo 'Password does not match!';
+ } else {
+ Session::get()->sign_in($user);
+ header("Location: /");
+ }
+ }
+ });
}
?>
</body>