Files
dog/php/php-dog/AuthMiddleware.php
Peter Hoppe a861acb2e0 useSWR
2023-01-30 17:01:44 +01:00

88 lines
2.5 KiB
PHP

<?php
//require __DIR__ . '/classes/JwtHandler.php';
require __DIR__ . '/classes/lib.php';
//class Auth extends JwtHandler
class Auth
{
protected $db;
protected $headers;
protected $token;
public function __construct($db, $headers)
{
//parent::__construct();
$this->db = $db;
$this->headers = $headers;
}
// public function isValid()
// {
// if (array_key_exists('Authorization', $this->headers) && preg_match('/Bearer\s(\S+)/', $this->headers['Authorization'], $matches)) {
// $data = $this->jwtDecodeData($matches[1]);
// if (
// isset($data['data']->user_id) &&
// $user = $this->fetchUser($data['data']->user_id)
// ) :
// return [
// "success" => 1,
// "user" => $user
// ];
// else :
// return [
// "success" => 0,
// "message" => $data['message'],
// ];
// endif;
// } else {
// return [
// "success" => 0,
// "message" => "Token not found in request"
// ];
// }
// }
public function isValid()
{
if(isset($_SESSION['user']))
{
$data = $_SESSION['user'];
if (
isset($data['id']) &&
$user = $this->fetchUser($data['id'])
) :
$retVal = new CMsg(1, 200, "User found", null, $user);
return $retVal->jsonarray();
else :
$retVal = new CMsg(0, 422, $data['message'], null, null);
return $retVal->jsonarray();
endif;
}
else
{
$retVal = new CMsg(0, 422, "User not found in request", null, null);
return $retVal->jsonarray();
}
}
protected function fetchUser($user_id)
{
try {
$fetch_user_by_id = "SELECT id, email, qr_id FROM dogs WHERE id=:id";
$query_stmt = $this->db->prepare($fetch_user_by_id);
$query_stmt->bindValue(':id', $user_id, PDO::PARAM_INT);
$query_stmt->execute();
if ($query_stmt->rowCount()) :
$returnVal = $query_stmt->fetch(PDO::FETCH_ASSOC);
return $returnVal;
else :
return null;
endif;
} catch (PDOException $e) {
return null;
}
}
}