<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/../config/config.php';
use Firebase\JWT\JWT;
use Firebase\JWT\Key;

function get_user_from_token() {
    if (!isset($_SERVER['HTTP_AUTHORIZATION'])) {
        return false;
    }
    $authHeader = $_SERVER['HTTP_AUTHORIZATION'];
    $parts = explode(' ', $authHeader);
    if (count($parts) !== 2 || $parts[0] !== 'Bearer') {
        return false;
    }
    $token = $parts[1];
    try {
        $decoded = JWT::decode($token, new Key(JWT_SECRET, 'HS256'));
        return $decoded->data;
    } catch (Exception $e) {
        return false;
    }
}
?>