CMS 2.0
This commit is contained in:
80
admin-console/src/Controllers/AuthController.php
Normal file
80
admin-console/src/Controllers/AuthController.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace CodePress\Admin\Controllers;
|
||||
|
||||
use CodePress\Admin\Services\AuthService;
|
||||
use CodePress\Admin\Services\LoggerService;
|
||||
|
||||
class AuthController {
|
||||
private AuthService $authService;
|
||||
private LoggerService $logger;
|
||||
|
||||
public function __construct() {
|
||||
$this->authService = new AuthService();
|
||||
$this->logger = new LoggerService();
|
||||
}
|
||||
|
||||
public function login() {
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$username = $_POST['username'] ?? '';
|
||||
$password = $_POST['password'] ?? '';
|
||||
$remember = isset($_POST['remember']);
|
||||
|
||||
$result = $this->authService->login($username, $password, $remember);
|
||||
|
||||
if ($result['success']) {
|
||||
$this->logger->info("User logged in: {$username}");
|
||||
$this->jsonResponse(['success' => true, 'redirect' => '/admin/dashboard']);
|
||||
} else {
|
||||
$this->logger->warning("Failed login attempt: {$username}");
|
||||
$this->jsonResponse(['success' => false, 'message' => $result['message']]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->renderView('auth/login');
|
||||
}
|
||||
|
||||
public function logout() {
|
||||
$this->authService->logout();
|
||||
$this->logger->info("User logged out");
|
||||
header('Location: /admin/login');
|
||||
exit;
|
||||
}
|
||||
|
||||
public function profile() {
|
||||
if (!$this->authService->isAuthenticated()) {
|
||||
header('Location: /admin/login');
|
||||
exit;
|
||||
}
|
||||
|
||||
$user = $this->authService->getCurrentUser();
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$email = $_POST['email'] ?? '';
|
||||
$currentPassword = $_POST['current_password'] ?? '';
|
||||
$newPassword = $_POST['new_password'] ?? '';
|
||||
|
||||
$result = $this->authService->updateProfile($user['id'], $email, $currentPassword, $newPassword);
|
||||
|
||||
if ($result['success']) {
|
||||
$this->logger->info("Profile updated: {$user['username']}");
|
||||
$this->jsonResponse(['success' => true, 'message' => 'Profile updated successfully']);
|
||||
} else {
|
||||
$this->jsonResponse(['success' => false, 'message' => $result['message']]);
|
||||
}
|
||||
}
|
||||
|
||||
$this->renderView('auth/profile', ['user' => $user]);
|
||||
}
|
||||
|
||||
private function jsonResponse(array $data) {
|
||||
header('Content-Type: application/json');
|
||||
echo json_encode($data);
|
||||
exit;
|
||||
}
|
||||
|
||||
private function renderView(string $view, array $data = []) {
|
||||
extract($data);
|
||||
require __DIR__ . "/../../public/templates/{$view}.php";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user