diff --git a/auth/logout.php b/auth/logout.php new file mode 100644 index 0000000..2eb94b1 --- /dev/null +++ b/auth/logout.php @@ -0,0 +1,90 @@ + 0, + 'path' => '/', + 'httponly' => true, + 'samesite' => 'Strict', + // 'secure' => true, +]); +session_start(); + +// Přijímáme pouze POST požadavky +if ($_SERVER['REQUEST_METHOD'] !== 'POST') { + header('Location: ' . AUTH_LOGIN_URL); + exit; +} + +// -- Ověření CSRF tokenu ------------------------------------- +$csrf_z_formulare = $_POST['csrf_token'] ?? ''; +$csrf_token = $_SESSION['csrf_token'] ?? ''; + +if (empty($csrf_token) || !hash_equals($csrf_token, $csrf_z_formulare)) { + // Neplatný CSRF token – přesměrujeme bez odhlášení + header('Location: ' . AUTH_LOGIN_URL); + exit; +} + +// -- Smazání remember me tokenu z DB ------------------------- +// Smažeme pouze token pro tento konkrétní selector (toto zařízení), +// ostatní zařízení uživatele zůstanou přihlášena +if (isset($_COOKIE['auth_selector'])) { + $stmt = $pdo->prepare(" + DELETE FROM `" . DB_TABULKA_TOKENY . "` + WHERE `selector` = :selector + "); + $stmt->execute([':selector' => $_COOKIE['auth_selector']]); +} + +// -- Smazání remember me cookies ----------------------------- +setcookie('auth_selector', '', [ + 'expires' => time() - 3600, + 'path' => '/', + 'httponly' => true, + 'samesite' => 'Strict', +]); +setcookie('auth_token', '', [ + 'expires' => time() - 3600, + 'path' => '/', + 'httponly' => true, + 'samesite' => 'Strict', +]); + +// -- Zrušení session ----------------------------------------- +// Vyprázdníme pole session +$_SESSION = []; + +// Smažeme session cookie z prohlížeče +if (ini_get('session.use_cookies')) { + $params = session_get_cookie_params(); + setcookie( + session_name(), + '', + time() - 3600, + $params['path'], + $params['domain'], + $params['secure'], + $params['httponly'] + ); +} + +// Zrušíme session na serveru +session_destroy(); + +// -- Přesměrování na přihlašovací stránku ------------------- +header('Location: ' . AUTH_LOGIN_URL); +exit; \ No newline at end of file