<?php
require_once __DIR__ . '/../../api/_jwt.php';
function get_bearer_token(){
    $headers = getallheaders();
    $auth = $headers['Authorization'] ?? $headers['authorization'] ?? null;
    if (!$auth) return null;
    if (preg_match('/Bearer\s+(.*)$/i', $auth, $m)) return $m[1];
    return null;
}
function require_auth(){
    $token = get_bearer_token();
    if (!$token) { http_response_code(401); echo json_encode(['error'=>'no_token']); exit; }
    $payload = verify_jwt($token);
    if (!$payload) { http_response_code(401); echo json_encode(['error'=>'invalid_token']); exit; }
    return $payload;
}
