<?php
// File: logout.php
session_start();
header('Content-Type: application/json');

// Include the reusable CORS handler
require_once __DIR__ . '/cors.php';

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    http_response_code(405);
    echo json_encode(['success' => false, 'message' => 'Only POST requests are allowed.']);
    exit;
}

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
if (ini_get("session.use_cookies")) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params["path"], $params["domain"],
        $params["secure"], $params["httponly"]
    );
}

// --- NEW CODE: Explicitly delete the dedicated authentication token ---
// We set the cookie's expiration to an hour in the past (-3600 seconds)
setcookie('auth_token', '', time() - 3600, '/');
// ---------------------------------------------------------------------

// Finally, destroy the session.
session_destroy();

echo json_encode(['success' => true, 'message' => 'Logged out successfully.']);
exit;
?>