2023-01-25 13:22:20 +00:00
|
|
|
<?php
|
|
|
|
|
namespace jobs\controllers;
|
|
|
|
|
class User {
|
|
|
|
|
private $usersTable;
|
|
|
|
|
private $catsTable;
|
|
|
|
|
private $vars;
|
|
|
|
|
|
2023-01-25 17:44:11 +00:00
|
|
|
public function __construct(\jobs\JobDatabaseTable $usersTable, \jobs\JobDatabaseTable $catsTable) {
|
2023-01-25 13:22:20 +00:00
|
|
|
$this->usersTable = $usersTable;
|
|
|
|
|
$this->catsTable = $catsTable;
|
|
|
|
|
$this->vars['cats'] = $this->catsTable->findAll();
|
|
|
|
|
$this->vars['response'] = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function login() {
|
|
|
|
|
return ['template' => 'login.html.php',
|
|
|
|
|
'title' => 'Jo\'s Jobs- Login',
|
|
|
|
|
'vars' => $this->vars];
|
|
|
|
|
}
|
2023-01-25 13:41:24 +00:00
|
|
|
|
|
|
|
|
public function loginSubmit() {
|
2023-01-25 15:36:10 +00:00
|
|
|
if ($_POST['username'] != '' && $_POST['password'] != '') {
|
2023-02-04 22:19:29 +00:00
|
|
|
$user = $this->usersTable->find(["username"], ['value0' => $_POST['username']]);
|
2023-01-25 17:02:57 +00:00
|
|
|
|
|
|
|
|
if (count($user) > 0 && $_POST['submit'] == 'Register') {
|
|
|
|
|
$this->vars['response'] = "Account already exists";
|
|
|
|
|
}
|
|
|
|
|
else if ($_POST['submit'] == "Register" && count($user) == 0) {
|
|
|
|
|
$record = ['username' => $_POST['username'],
|
|
|
|
|
'password' => password_hash($_POST['password'], PASSWORD_DEFAULT),
|
|
|
|
|
'userType' => 'client'];
|
|
|
|
|
$this->usersTable->save($record);
|
|
|
|
|
$this->vars['response'] = 'You have now been registered';
|
|
|
|
|
}
|
|
|
|
|
else if ($_POST['submit'] == "Log In" && password_verify($_POST['password'], $user[0]->password)) {
|
|
|
|
|
$user = $user[0];
|
2023-01-25 13:41:24 +00:00
|
|
|
$_SESSION['loggedin'] = $user->id;
|
2023-01-25 13:54:46 +00:00
|
|
|
$_SESSION['userType'] = $user->userType;
|
2023-01-25 13:41:24 +00:00
|
|
|
$this->vars['response'] = 'You are now logged in';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
unset($_SESSION['loggedin']);
|
2023-01-25 13:54:46 +00:00
|
|
|
unset($_SESSION['userType']);
|
2023-01-25 13:41:24 +00:00
|
|
|
$this->vars['response'] = 'Login Unsuccessful';
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if ($_POST['username'] == '') {
|
|
|
|
|
$this->vars['response'] .= "No Username was entered \n";
|
|
|
|
|
}
|
|
|
|
|
if ($_POST['password'] == '') {
|
2023-02-03 20:16:28 +00:00
|
|
|
$this->vars['response'] .= "No Password was entered \n";
|
2023-01-25 13:41:24 +00:00
|
|
|
}
|
|
|
|
|
$this->vars['response'] .= 'Login Unsuccessful';
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-25 16:21:56 +00:00
|
|
|
return ['template' => 'response.html.php',
|
2023-01-25 13:41:24 +00:00
|
|
|
'title' => 'Jo\'s Jobs- Login',
|
|
|
|
|
'vars' => $this->vars
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function logout() {
|
|
|
|
|
unset($_SESSION['loggedin']);
|
2023-01-25 13:54:46 +00:00
|
|
|
unset($_SESSION['userType']);
|
2023-01-25 13:41:24 +00:00
|
|
|
$this->vars['response'] = 'Logged Out Successfully';
|
|
|
|
|
|
|
|
|
|
return ['template' => 'response.html.php',
|
|
|
|
|
'title' => 'Jo\'s Jobs- Logged Out',
|
|
|
|
|
'vars' => $this->vars];
|
|
|
|
|
}
|
2023-01-25 17:14:23 +00:00
|
|
|
}
|
|
|
|
|
?>
|