
<?php

require_once __DIR__ . '/jwt.php';
require_once __DIR__ . '/../core/response.php';

class ProviderAuth
{

    public static function id()
    {

        $headers = getallheaders();

        if(!isset($headers['Authorization'])){

            Response::error("Authorization header missing",401);

        }

        $auth = $headers['Authorization'];

        if(!str_starts_with($auth,'Bearer ')){

            Response::error("Invalid authorization format",401);

        }

        $token = str_replace('Bearer ','',$auth);

        $payload = JWTAuth::verify($token);

        if(!$payload || !isset($payload->provider_id)){

            Response::error("Invalid token",401);

        }

        return $payload->provider_id;

    }

}
