php jede menge
This commit is contained in:
98
php/php-dog/AuthMiddleware.php
Normal file
98
php/php-dog/AuthMiddleware.php
Normal file
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
//require __DIR__ . '/classes/JwtHandler.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['data']->id) &&
|
||||
$user = $this->fetchUser($data['data']->id)
|
||||
) :
|
||||
return [
|
||||
"success" => 1,
|
||||
"message" => "User found",
|
||||
"user" => $user
|
||||
];
|
||||
else :
|
||||
return [
|
||||
"success" => 0,
|
||||
"message" => $data['message'],
|
||||
"user" => null
|
||||
];
|
||||
endif;
|
||||
}
|
||||
else
|
||||
{
|
||||
return [
|
||||
"success" => 0,
|
||||
"message" => "User not found in request",
|
||||
"user" => null
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
protected function fetchUser($user_id)
|
||||
{
|
||||
try {
|
||||
$fetch_user_by_id = "SELECT id, email, name, 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);
|
||||
$returnVal->session = $_SESSION;
|
||||
return $returnVal;
|
||||
else :
|
||||
return false;
|
||||
endif;
|
||||
} catch (PDOException $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user