<?php
// /api/middleware.php
require_once __DIR__ . '/../_Core/bootstrap.php';
require_once __DIR__ . '/../_Core/Helpers.php';
require_once __DIR__ . '/../_Core/TokenMiddleware.php';

function api_authenticate_or_die() {
    // Returns user id on success, or exits with JSON error
    if (!TokenMiddleware::authenticate()) {
        header('Content-Type: application/json');
        http_response_code(401);
        echo json_encode(['status'=>0, 'error'=>'not_authenticated']);
        exit;
    }
    $uid = $_SERVER['AUTH_USER_ID'] ?? null;
    if (!$uid) {
        header('Content-Type: application/json');
        http_response_code(401);
        echo json_encode(['status'=>0, 'error'=>'not_authenticated']);
        exit;
    }
    return $uid;
}
?>
