Changed login/logout process

This commit is contained in:
2017-09-08 10:05:42 +07:00
parent 8eb9005642
commit 8956b17434
6 changed files with 148 additions and 212 deletions

View File

@@ -10,11 +10,16 @@ use Core\Hash;
class Home
{
private $access;
public $access,
$table;
public function __construct()
{
$this->access = new Access();
$this->table = 'user';
}
/* Routes */
public function index()
{
$posts = new Posts();
@@ -33,6 +38,21 @@ class Home
}
}
public function logout() {
if ($this->delete() != true) {
$info = "There's an error. Please try again.";
} else {
Session::delete('userid');
Session::delete('username');
Session::delete('full_name');
Session::delete('privilage');
$info = "Logged out success";
}
Session::flash('info', $info);
Redirect::to('/');
}
public function register()
{
if (Session::exists('userid') && Session::get('privilage') == 1) {
@@ -44,7 +64,7 @@ class Home
}
}
// Methods
/* Methods */
public function post($args = [])
{
foreach ($args as $value) {
@@ -69,7 +89,7 @@ class Home
$args['full_name'] = htmlspecialchars($args['full_name']);
$args['username'] = htmlspecialchars($args['username']);
$data = $this->access->showAll();
$data = $this->access->showAll($table);
foreach ($data as $users) {
if ($args['username'] == $users['username']) {
Session::flash('info', 'Username already exists');
@@ -85,38 +105,43 @@ class Home
public function put($args = [])
{
if ($user = $this->access->login($args)) {
Session::put('userid', $user['id']);
Session::put('username', $user['username']);
Session::put('full_name', $user['full_name']);
Session::put('privilage', $user['privilage']);
$table = 'user';
$username = $args['username'];
$password = $args['password'];
$table = 'user';
$id = Session::get('userid');
if ($this->access->update($table, ['status' => 1], $id)) {
$username = Session::get('username');
Session::flash('info', "$username logged in");
}
$user = $this->access->showAll($table, [
['username', '=', $username]
]);
if ($user == false) {
$info = "Invalid username/password";
} else {
Session::flash('info', 'Invalid username/password');
$hash = Hash::compare($password, $user['salt'], $user['password']);
if ($hash == true) {
if ($this->access->update($table, ['status' => 1], $user['id']) != true) {
$info = "There's an error. Please try again.";
} else {
Session::put('userid', $user['id']);
Session::put('username', $user['username']);
Session::put('full_name', $user['full_name']);
Session::put('privilage', $user['privilage']);
$info = "Logged in success";
}
}
}
Session::flash('info', $info);
Redirect::to('/');
}
public function logout()
public function delete()
{
$table = 'user';
$user = Session::get('userid');
$username = Session::get('username');
if ($this->access->logout($user)) {
Session::flash('info', "$username has logged out");
Session::delete('userid');
Session::delete('username');
Session::delete('full_name');
Session::delete('privilage');
Redirect::to('/');
if ($this->access->update($table, ['status' => 0], $user) != true) {
throw new \Exception("Bad request", 400);
}
return true;
}
}