ApiControler

This commit is contained in:
xinatorus 2020-04-24 21:22:24 +02:00
parent 84127bf7ab
commit b4cfffc432
1 changed files with 27 additions and 20 deletions

View File

@ -1,30 +1,37 @@
<?php <?php
class ApiCOntroller { class ApiController {
private $data = []; private $input;
public $httpCode = 200; private $authenticated;
public $autenticated = false;
function __construct() { function __construct() {
$this->headers = $_SERVER; $this->authenticated = false;
}
$input = file_get_contents('php://input');
if(empty($input)){
$this->input = NULL;
}else{
$this->input = json_decode($input, true);
if(json_last_error() != JSON_ERROR_NONE){
throw new Exception("Invalid request payload", 400);
}
}
}
function requireAuth(){ function requireAuth(){
if (isset($this->headers['HTTP_AUTHORIZATION'])) { if (isset($this->headers['HTTP_AUTHORIZATION'])) {
$this->autenticated = $this->apiManager->validateToken(explode(' ', $this->headers['HTTP_AUTHORIZATION'])[1]); // TODO: call appropriate class/method
$authManager = new AuthManager();
$this->authenticated = $authManager>validateToken($this->headers['HTTP_AUTHORIZATION']);
if(!$this->authenticated){
throw new Exception("Auth required", 401);
}
} else { } else {
$error = new ApiError(); throw new Exception("Auth required", 401);
$error->code = "missing_token_header";
$error->message = "Missing Token in Header";
$error->hint = "check paiload header for 'token'";
echo json_encode($error);
die();
} }
} }
function response($data = [], $httpCode = '200'){ function response($data = [], $httpCode = '200'){
http_response_code($httpCode); http_response_code($httpCode);
echo json_encode($data); echo json_encode($data);
die();
} }
} }