Files
aleshell2/src/controllers/AuthController.php
2025-09-23 22:57:23 +02:00

60 líneas
1.6 KiB
PHP

<?php
/**
* Authentication Controller
*
* Handles user authentication
*/
namespace AleShell\Controllers;
use AleShell\Controllers\BaseController;
class AuthController extends BaseController
{
public function login(): void
{
$data = $this->getRequestData();
$password = $data['password'] ?? '';
if (empty($password)) {
$this->errorResponse('Password is required');
return;
}
try {
if ($this->security->authenticate($password)) {
$this->successResponse([
'authenticated' => true,
'csrf_token' => $this->security->generateCSRFToken(),
'redirect' => '/dashboard'
]);
} else {
$this->errorResponse('Invalid password', 401);
}
} catch (\Exception $e) {
$this->errorResponse($e->getMessage(), 401);
}
}
public function logout(): void
{
$this->security->logout();
$this->successResponse(['logged_out' => true]);
}
public function status(): void
{
// Use the working direct output but with proper API structure
$token = $this->security->generateCSRFToken();
$data = [
'authenticated' => $this->security->isAuthenticated(),
'csrf_token' => $token
];
$response = ['success' => true, 'data' => $data];
header('Content-Type: application/json');
echo json_encode($response);
exit;
}
}