<?php
declare(strict_types=1);

function json_response(array $payload, int $status = 200): never {
  http_response_code($status);
  header('Content-Type: application/json; charset=utf-8');
  header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
  echo json_encode($payload, JSON_UNESCAPED_UNICODE);
  exit;
}

function start_fp_session(): void {
  if (session_status() === PHP_SESSION_ACTIVE) return;
  session_start();
}

start_fp_session();

if (!isset($_SESSION["fp_user"])) {
  json_response(["ok" => false, "message" => "No autenticado."], 401);
}

json_response(["ok" => true, "user" => $_SESSION["fp_user"]], 200);
