90 lines
3.2 KiB
PHP
Executable File
90 lines
3.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Filters;
|
|
|
|
use App\Helpers\JWTToken;
|
|
use CodeIgniter\Filters\FilterInterface;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Firebase\JWT\JWT;
|
|
use Firebase\JWT\Key;
|
|
use ReflectionClass;
|
|
|
|
require_once('../vendor/autoload.php');
|
|
|
|
use App\Models\EmployeeModel;
|
|
use App\Models\LevelContactModel;
|
|
|
|
|
|
class AuthJWT implements FilterInterface
|
|
{
|
|
public function before(RequestInterface $request, $arguments = null)
|
|
{
|
|
$jwt = $request->getHeader('Authorization');
|
|
|
|
if ($jwt) {
|
|
if (JWTToken::validateJWT($jwt)) {
|
|
$data = JWTToken::validateJWT($jwt);
|
|
$data = json_decode($data);
|
|
|
|
$id = $data->decoded->id;
|
|
if(isset($data->decoded->emp_code)){
|
|
$model = new EmployeeModel();
|
|
$user_data = $model->where('id', $id)->first();
|
|
|
|
if($user_data['token_time_out'] > time()){
|
|
$data =["token_time_out" => time() + getenv('TOKENTIMEOUT') ];
|
|
$model->update($id, $data);
|
|
return true;
|
|
}else{
|
|
|
|
// if($user_data['token_time_out'] != "" && $user_data['token_time_out'] != NULL)
|
|
|
|
$data =["token_time_out" => ''];
|
|
$model->update($id, $data);
|
|
header('Content-Type: application/json');
|
|
http_response_code(401);
|
|
// $error = json_encode(["status" => 401, "message" => $data->message]);
|
|
$error = json_encode(["status" => 401, "message" => "Token is Invalid"]);
|
|
echo $error;
|
|
exit;
|
|
}
|
|
}else{
|
|
$model = new LevelContactModel();
|
|
$hr_data = $model->where('id', $id)->first();
|
|
|
|
if($hr_data['token_time_out'] > time()){
|
|
$data =["token_time_out" => time() + getenv('TOKENTIMEOUT') ];
|
|
$model->update($id, $data);
|
|
return true;
|
|
}else{
|
|
$data =["token_time_out" => ''];
|
|
$model->update($id, $data);
|
|
header('Content-Type: application/json');
|
|
http_response_code(401);
|
|
// $error = json_encode(["status" => 401, "message" => $data->message]);
|
|
$error = json_encode(["status" => 401, "message" => "Token is Invalid"]);
|
|
echo $error;
|
|
exit;
|
|
}
|
|
}
|
|
|
|
}
|
|
} else {
|
|
header('Content-Type: application/json');
|
|
http_response_code(403);
|
|
$error = json_encode(["status" => 403, "message" => "Access Forbidden!"]);
|
|
echo $error;
|
|
exit();
|
|
}
|
|
}
|
|
|
|
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
|
|
{
|
|
// Do something here after the response is sent
|
|
}
|
|
|
|
|
|
|
|
}
|