summaryrefslogtreecommitdiff
path: root/signin.php
blob: 4f76d13d6efe8402804b71eb7fc1eea1c97c125e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php session_start() ?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Sign in - cflip.net forum</title>
<?php include_once 'includes/templates/head.php'; ?>
	</head>
	<body>
<?php include_once './includes/templates/header.php' ?>
		<h2>Sign in</h2>
		<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
			<label for="user_name">Username: </label><br>
			<input type="text" name="user_name"><br>
			<label for="user_pass">Password: </label><br>
			<input type="password" name="user_pass"><br>
			<input type="submit" name="submit">
		</form>
<?php
include_once './includes/form/SignInForm.php';
include_once './includes/error.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $form = new SignInForm();
    $username = $form->validate_username($_POST['user_name']);
	$password = $form->validate_password($_POST['user_pass']);

    $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($password, $user->password)) {
                echo 'Password does not match!';
            } else {
                Session::get()->sign_in($user);
                header("Location: /");
            }
        }
    });
}
?>
	</body>
</html>