118 lines
3.1 KiB
PHP
118 lines
3.1 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../models/User.php';
|
|
|
|
class AuthController
|
|
{
|
|
public function loginForm(): void
|
|
{
|
|
$error = $_SESSION['error'] ?? null;
|
|
unset($_SESSION['error']);
|
|
require __DIR__ . '/../views/login.php';
|
|
}
|
|
|
|
public function login(): void
|
|
{
|
|
$login = trim($_POST['login'] ?? '');
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($login) || empty($password)) {
|
|
$_SESSION['error'] = 'Proszę podać login i hasło.';
|
|
header('Location: /logowanie');
|
|
exit;
|
|
}
|
|
|
|
$user = User::verifyPassword($login, $password);
|
|
|
|
if (!$user) {
|
|
$_SESSION['error'] = 'Nieprawidłowy login lub hasło.';
|
|
header('Location: /logowanie');
|
|
exit;
|
|
}
|
|
|
|
// Generowanie kodu weryfikacyjnego
|
|
$code = sprintf('%06d', random_int(0, 999999));
|
|
|
|
User::saveVerificationCode($user['id'], $code);
|
|
|
|
// Symulacja wysyłki maila
|
|
$_SESSION['pending_user_id'] = $user['id'];
|
|
$_SESSION['pending_user_login'] = $user['login'];
|
|
$_SESSION['simulated_code'] = $code; // W produkcji nie pokazywać!
|
|
|
|
header('Location: /weryfikacja');
|
|
exit;
|
|
}
|
|
|
|
public function verifyForm(): void
|
|
{
|
|
if (!isset($_SESSION['pending_user_id'])) {
|
|
header('Location: /logowanie');
|
|
exit;
|
|
}
|
|
|
|
$error = $_SESSION['error'] ?? null;
|
|
$simulatedCode = $_SESSION['simulated_code'] ?? null;
|
|
$userLogin = $_SESSION['pending_user_login'] ?? '';
|
|
unset($_SESSION['error']);
|
|
|
|
require __DIR__ . '/../views/verify.php';
|
|
}
|
|
|
|
public function verify(): void
|
|
{
|
|
if (!isset($_SESSION['pending_user_id'])) {
|
|
header('Location: /logowanie');
|
|
exit;
|
|
}
|
|
|
|
$code = trim($_POST['code'] ?? '');
|
|
$userId = $_SESSION['pending_user_id'];
|
|
|
|
if (empty($code)) {
|
|
$_SESSION['error'] = 'Proszę podać kod weryfikacyjny.';
|
|
header('Location: /weryfikacja');
|
|
exit;
|
|
}
|
|
|
|
if (!User::verifyCode($userId, $code)) {
|
|
$_SESSION['error'] = 'Nieprawidłowy lub wygasły kod weryfikacyjny.';
|
|
header('Location: /weryfikacja');
|
|
exit;
|
|
}
|
|
|
|
// Usunięcie kodu po użyciu
|
|
User::deleteVerificationCode($userId);
|
|
|
|
// Pełna autoryzacja
|
|
$_SESSION['user_id'] = $userId;
|
|
$_SESSION['user_login'] = $_SESSION['pending_user_login'];
|
|
$_SESSION['logged_in'] = true;
|
|
|
|
unset($_SESSION['pending_user_id']);
|
|
unset($_SESSION['pending_user_login']);
|
|
unset($_SESSION['simulated_code']);
|
|
|
|
header('Location: /panel');
|
|
exit;
|
|
}
|
|
|
|
public function dashboard(): void
|
|
{
|
|
if (!isset($_SESSION['logged_in']) || !$_SESSION['logged_in']) {
|
|
header('Location: /logowanie');
|
|
exit;
|
|
}
|
|
|
|
$userLogin = $_SESSION['user_login'] ?? 'Użytkownik';
|
|
require __DIR__ . '/../views/dashboard.php';
|
|
}
|
|
|
|
public function logout(): void
|
|
{
|
|
session_destroy();
|
|
header('Location: /logowanie');
|
|
exit;
|
|
}
|
|
}
|