47 lines
1.1 KiB
PHP
Executable File
47 lines
1.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Controllers;
|
|
|
|
use Core\Controller;
|
|
use Models\UserModel;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
private $userModel;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->userModel = new UserModel();
|
|
}
|
|
|
|
public function login()
|
|
{
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = $_POST['username'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
$user = $this->userModel->getByUsername($username);
|
|
if ($user && password_verify($password, $user['password_hash'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
header('Location: ' . HOME_DIRECTORY . 'index.php?url=admin/index');
|
|
exit;
|
|
} else {
|
|
$error = "Incorrect login or password";
|
|
$this->view->render('admin/login.php', ['error' => $error]);
|
|
return;
|
|
}
|
|
}
|
|
|
|
$this->view->render('admin/login.php');
|
|
}
|
|
|
|
public function logout()
|
|
{
|
|
unset($_SESSION['user_id']);
|
|
header('Location: ' . HOME_DIRECTORY . 'index.php');
|
|
exit;
|
|
}
|
|
}
|
|
|