-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifyUser.php
68 lines (59 loc) · 1.98 KB
/
verifyUser.php
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/* program to verify login*/
require_once __DIR__ . '/config/apiReturn.php';
require_once __DIR__ . '/config/authenticate.php';
require_once __DIR__ . '/repository/database.php';
require_once __DIR__ . '/repository/verifyUserRepo.php';
/* Set required header and session start */
requiredHeaderAndSessionStart();
/* Connect to database */
$conn = getConnection();
if (validateUser($conn)) {
logout($conn);
}
if (isValidPostVar('username') && isValidPostVar('userType') && isValidPostVar('password')) {
/* Store user type in session */
$userType = trim($_POST['userType']);
$_SESSION['userType'] = $userType;
$email = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
$remember = $_POST['remember'] ?? true;
if (!(verifyUserType($userType))) {
/* Exit and tell the client that their user type is invalid */
exit(invalidUserType($userType));
}
$rows = queryUserVerify($email, $userType, $conn);
$row = getArrayFromResult($rows);
$loginInfo = (object)[
'email' => $email,
'password' => $password,
'userType' => $userType,
'remember' => $remember,
'hashedPassword' => $row['password'],
];
/* Make sure the password is correct */
if (login($loginInfo, $conn)) {
/* If we are determining user from table then determine it */
$determineUser = strcmp($userType, 'user') == 0;
if ($determineUser) {
$_SESSION['userType'] = $row['userType'];
}
$_SESSION['userID'] = getUserID($conn);
$_SESSION['username'] = htmlentities($row['fname'] . ' ' . $row['lname']);
echo authenticatedSuccessfully($_SESSION['userType']);
} else {
http_response_code(403);
header('HTTP/1.0 403 Forbidden');
echo INVALID_PASSWORD;
}
} else {
echo MISSING_PARAMETERS;
}
$conn = null;
function getArrayFromResult($rows)
{
if (count($rows) == 0) {
exit (USERNAME_NOT_IN_TABLE);
}
return $rows[0];
}